EMAN2
util.h
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__util_h__
33#define eman__util_h__ 1
34
35#ifdef WIN32
36#include <windows.h>
37 #include <process.h>
38 #define M_PI 3.14159265358979323846f
39 #define MUTEX HANDLE
40//#define MAXPATHLEN (MAX_PATH * 4)
41#else
42#include <pthread.h>
43#define MUTEX pthread_mutex_t
44#endif
45
46#include <vector>
47#include <iostream>
48
49#include <string>
50using std::string;
51
52#include "sparx/emconstants.h"
53#include "exception.h"
54
55#include <boost/multi_array.hpp>
56#include "vec3.h"
57
58using std::string;
59using std::vector;
60using std::ostream;
61using std::cout;
62using std::endl;
63
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 };
77
78
79namespace EMAN
80{
81 class EMData;
82 class Dict;
83
84 typedef boost::multi_array<int, 3> MIArray3D;
85
89 class Util
90 {
92 #include "sparx/util_sparx.h"
93 #include "sphire/util_sphire.h"
94
95 public:
96
97 //Functions do Cross-Platform Mutex
98 static int MUTEX_INIT(MUTEX *mutex);
99 static int MUTEX_LOCK(MUTEX *mutex);
100 static int MUTEX_UNLOCK(MUTEX *mutex);
101
105 static inline bool is_nan(const float number)
106 {
107 return std::isnan(number);
108 }
109
115 static void ap2ri(float *data, size_t n);
116 static void ap2ri(double *data, size_t n);
117
122 static void flip_complex_phase(float *data, size_t n);
123
131 static void rotate_phase_origin(float *data, size_t nx, size_t ny, size_t nz);
132
140 static int file_lock_wait(FILE * file);
141 //static void file_unlock(FILE * file);
142
148 static bool check_file_by_magic(const void *first_block, const char *magic);
149
153 static bool is_file_exist(const string & filename);
154
160 static void flip_image(float *data, size_t nx, size_t ny);
161
167 static vector<EMData *> svdcmp(const vector<EMData *> &data,int nvec);
168
173 static string str_to_lower(const string& s);
174
180 static void replace_non_ascii(char *str, int max_size, char repl_char='?');
181
190 static bool sstrncmp(const char *s1, const char *s2);
191
196 static string int2str(int n);
197
206 static string get_line_from_string(char **str);
207
219 static bool get_str_float(const char *str, const char *float_var, float *p_val);
220
233 static bool get_str_float(const char *str, const char *float_var,
234 float *p_v1, float *p_v2);
235
254 static bool get_str_float(const char *str, const char *float_var,
255 int *p_nvalues, float *p_v1, float *p_v2);
256
268 static bool get_str_int(const char *str, const char *int_var, int *p_val);
269
282 static bool get_str_int(const char *str, const char *int_var,
283 int *p_v1, int *p_v2);
284
303 static bool get_str_int(const char *str, const char *int_var,
304 int *p_nvalues, int *p_v1, int *p_v2);
305
316 static string change_filename_ext(const string& old_filename,
317 const string & new_ext);
318
325 static string remove_filename_ext(const string& filename);
326
332 static string get_filename_ext(const string& filename);
333
340 static string sbasename(const string & filename);
341
354 static void calc_least_square_fit(size_t nitems, const float *data_x,
355 const float *data_y, float *p_slope,
356 float *p_intercept, bool ignore_zero,float absmax=0);
357
364 static Vec3f calc_bilinear_least_square(const vector<float> &points);
365
376 static void save_data(const vector < float >&x_array,
377 const vector < float >&y_array,
378 const string & filename);
379
388 static void save_data(float x0, float dx,
389 const vector < float >&y_array,
390 const string & filename);
391
401 static void save_data(float x0, float dx, float *y_array,
402 size_t array_size, const string & filename);
403
412 static void sort_mat(float *left, float *right, int *leftPerm,
413 int *rightPerm);
414
418 static unsigned long long get_randnum_seed();
419
423 static void set_randnum_seed(unsigned long long seed);
424
430 static int get_irand(int low, int high);
431
437 static float get_frand(int low, int high);
438
444 static float get_frand(float low, float high);
445
451 static float get_frand(double low, double high);
452
459 static float get_gauss_rand(float mean, float sigma);
460
465 static inline int round(float x)
466 {
467 if (x < 0) {
468 return (int) (x - 0.5f);
469 }
470 return (int) (x + 0.5f);
471 }
472
477 static inline int round(double x)
478 {
479 if (x < 0) {
480 return (int) (x - 0.5);
481 }
482 return (int) (x + 0.5);
483 }
484
489 static inline float log_2(float x)
490 {
491 return (float) (log(x) / log(2.0f));
492 }
493
498 static inline double log_2(double x)
499 {
500 return (double) (log(x) / log(2.0));
501 }
502
507 static inline float log_2(int i)
508 {
509 return (float) (log((float)i) / log(2.0f));
510 }
511
518 static inline float linear_interpolate(float p1, float p2, float t)
519 {
520 return (1-t) * p1 + t * p2;
521 }
522
529 static inline std::complex<float> linear_interpolate_cmplx(std::complex<float> p1, std::complex<float> p2, float t)
530 {
531 return (1.0f-t) * p1 + t * p2;
532 }
533
543 static inline float bilinear_interpolate(float p1, float p2, float p3,
544 float p4, float t, float u)
545 {
546 return (1-t) * (1-u) * p1 + t * (1-u) * p2 + (1-t) * u * p3 + t * u * p4;
547 }
548
558 static inline std::complex<float> bilinear_interpolate_cmplx(std::complex<float> p1, std::complex<float> p2, std::complex<float> p3,
559 std::complex<float> p4, float t, float u)
560 {
561 return (1.0f-t) * (1.0f-u) * p1 + t * (1.0f-u) * p2 + (1.0f-t) * u * p3 + t * u * p4;
562 }
563
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)
575 {
576 float c4 = Util::fast_gauss_B2(1.0f-t)*Util::fast_gauss_B2(1.0f-u);
577 float c3 = Util::fast_gauss_B2(t)*Util::fast_gauss_B2(1.0f-u);
578 float c2 = Util::fast_gauss_B2(1.0f-t)*Util::fast_gauss_B2(u);
580 return (c1 * p1 + c2 * p2 + c3 * p3 + c4 * p4)/(c1+c2+c3+c4);
581 }
582
589 static inline std::complex<float> gauss3_interpolate_cmplx(std::complex<float> *p, float t, float u)
590 {
591 std::complex<float> sm=0;
592 float wt=0;
593 for (int y=0,i=0; y<3; y++) {
594 for (int x=0; x<3; x++,i++) {
596 sm+=c*p[i];
597 wt+=c;
598 }
599 }
600 return sm/wt;
601 }
602
603
619 static inline float trilinear_interpolate(float p1, float p2, float p3,
620 float p4, float p5, float p6,
621 float p7, float p8, float t,
622 float u, float v)
623 {
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);
628 }
629
645 static inline std::complex<float> trilinear_interpolate_complex(std::complex<float> p1, std::complex<float> p2, std::complex<float> p3,
646 std::complex<float> p4, std::complex<float> p5, std::complex<float> p6,
647 std::complex<float> p7, std::complex<float> p8, float t,
648 float u, float v)
649 {
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);
654 }
655
656
663 static void find_max(const float *data, size_t nitems,
664 float *p_max_val, int *p_max_index = 0);
665
676 static void find_min_and_max(const float *data, size_t nitems,
677 float *p_max_val, float *p_min_val,
678 int *p_max_index = 0, int *p_min_index = 0);
679
684 static Dict get_stats( const vector<float>& data );
685
689 static Dict get_stats_cstyle( const vector<float>& data );
690
697 static int calc_best_fft_size(int low);
698
706 static vector<float> nonconvex(const vector<float>&curve,int first=3);
707
718 static vector<float> windowdot(const vector<float>&curveA,const vector<float>&curveB,int window,int normA);
719
720
721 static EMData* calc_bessel(const int n, const float& x);
722
724 static inline double angle3(double x1,double y1, double z1,double x2, double y2, double z2) {
725 return fast_acos((x1*x2+y1*y2+z1*z2)/(hypot3(x1,y1,z1)*hypot3(x2,y2,z2))); //fast_acos tolerates roundoff error slightly outside -1 to 1 range
726 }
727
728 static inline float angle3(float x1,float y1, float z1,float x2, float y2, float z2) {
729 return fast_acos((x1*x2+y1*y2+z1*z2)/(hypot3(x1,y1,z1)*hypot3(x2,y2,z2)));
730 }
731
736 static inline int square(int n)
737 {
738 return (n * n);
739 }
740
745 static inline float square(float x)
746 {
747 return (x * x);
748 }
749
754 static inline float square(double x)
755 {
756 return (float)(x * x);
757 }
758
764 static inline float square_sum(float x, float y)
765 {
766 return (float)(x * x + y * y);
767 }
768
774 static inline float hypot2(float x, float y)
775 {
776 return (float) hypot(x, y);
777 }
778
784 static inline int hypot2sq(int x, int y)
785 {
786 return (x * x + y * y);
787 }
788
794 static inline float hypot2sq(float x, float y)
795 {
796 return (float)(x * x + y * y);
797 }
798
805 static inline int hypot3sq(int x, int y, int z)
806 {
807 return ((x * x + y * y + z * z));
808 }
809
816 static inline float hypot3sq(float x, float y, float z)
817 {
818 return (x * x + y * y + z * z);
819 }
820
827 static inline float hypot3(int x, int y, int z)
828 {
829 return sqrtf((float)(x * x + y * y + z * z));
830 }
831
838 static inline float hypot3(float x, float y, float z)
839 {
840 return sqrtf(x * x + y * y + z * z);
841 }
842
849 static inline double hypot3(double x, double y, double z)
850 {
851 return (double) sqrt(x * x + y * y + z * z);
852 }
853
859 static float hypot_fast(int x, int y);
860
866 static short hypot_fast_int(int x, int y);
867
874 static inline int fast_floor(float x)
875 {
876 if (x < 0) {
877 return ((int) x - 1);
878 }
879 return (int) x;
880 }
881
887 static float fast_exp(const float &f) ;
888
894 static float fast_acos(const float &f) ;
895
901 static float fast_gauss_B2(const float &f) ;
902
903
912 static inline float agauss(float a, float dx, float dy, float dz, float d)
913 {
914 return (a * exp(-(dx * dx + dy * dy + dz * dz) / d));
915 }
916
922 static inline int get_min(int f1, int f2)
923 {
924 return (f1 < f2 ? f1 : f2);
925 }
926
933 static inline int get_min(int f1, int f2, int f3)
934 {
935 if (f1 <= f2 && f1 <= f3) {
936 return f1;
937 }
938 if (f2 <= f1 && f2 <= f3) {
939 return f2;
940 }
941 return f3;
942 }
943
949 static inline float get_min(float f1, float f2)
950 {
951 return (f1 < f2 ? f1 : f2);
952 }
953
960 static inline float get_min(float f1, float f2, float f3)
961 {
962 if (f1 <= f2 && f1 <= f3) {
963 return f1;
964 }
965 if (f2 <= f1 && f2 <= f3) {
966 return f2;
967 }
968 return f3;
969 }
970
978 static inline float get_min(float f1, float f2, float f3, float f4)
979 {
980 float m = f1;
981 if (f2 < m) {
982 m = f2;
983 }
984 if (f3 < m) {
985 m = f3;
986 }
987 if (f4 < m) {
988 m = f4;
989 }
990 return m;
991 }
992
998 static inline float get_max(float f1, float f2)
999 {
1000 return (f1 < f2 ? f2 : f1);
1001 }
1002
1009 static inline float get_max(float f1, float f2, float f3)
1010 {
1011 if (f1 >= f2 && f1 >= f3) {
1012 return f1;
1013 }
1014 if (f2 >= f1 && f2 >= f3) {
1015 return f2;
1016 }
1017 return f3;
1018 }
1019
1027 static inline float get_max(float f1, float f2, float f3, float f4)
1028 {
1029 float m = f1;
1030 if (f2 > m) {
1031 m = f2;
1032 }
1033 if (f3 > m) {
1034 m = f3;
1035 }
1036 if (f4 > m) {
1037 m = f4;
1038 }
1039 return m;
1040 }
1041
1043 static inline float angle_norm_2pi(float in)
1044 {
1045 float m = fmod((float)in, (float)(2.0*M_PI));
1046
1047 return m<0?m+2.0*M_PI:m;
1048 }
1049
1051 static inline float angle_norm_pi(float in)
1052 {
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;
1056 }
1057
1058
1066 static inline float angle_sub_2pi(float x, float y)
1067 {
1068 float r = fmod(fabs(x - y), (float) (2.0 * M_PI));
1069 if (r<0) r+=2.0*M_PI;
1070 if (r > M_PI) {
1071 r = (float) (2.0 * M_PI - r);
1072 }
1073
1074 return r;
1075 }
1076
1084 static inline float angle_sub_pi(float x, float y)
1085 {
1086 float r = fmod(fabs(x - y), (float) M_PI);
1087 if (r > M_PI / 2.0) {
1088 r = (float)(M_PI - r);
1089 }
1090 return r;
1091 }
1092
1099 static inline float angle_err_ri(float r1,float i1,float r2,float i2) {
1100 if ((r1==0 && i1==0) || (r2==0 && i2==0)) return 0;
1101// printf("%f\t%f\t%f\n",r1*r2+i1*i2,hypot(r1,i1),hypot(r2,i2));
1102// return acos((r1*r2+i1*i2)/(hypot(r1,i1)*hypot(r2,i2)));
1103 return fast_acos((r1*r2+i1*i2)/(float)(hypot(r1,i1)*hypot(r2,i2))); // fast_acos also tolerates values very slightly above 1
1104 }
1105
1112 static inline int goodf(const float *p_f)
1113 {
1114 // The old code used 'fpclassify' which was demonstrably broken on many platforms,
1115 // causing many irritating problems
1116
1117 if (((*((int *)p_f)>>23)&255)==255) return 0;
1118
1119 return 1;
1120 }
1121
1122 static inline int goodf(const double *p_f)
1123 {
1124 // The old code used 'fpclassify' which was demonstrably broken on many platforms,
1125 // causing many irritating problems
1126
1127 if (((*((long long *)p_f)>>52)&2047)==2047) return 0;
1128
1129 return 1;
1130 }
1131
1132#ifndef _WIN32
1133 static string recv_broadcast(int port);
1134#endif //_WIN32
1135
1139 static string get_time_label();
1140
1147 static void set_log_level(int argc, char *argv[]);
1148
1159 static inline float eman_copysign(float a, float b)
1160 {
1161#ifndef WIN32
1162 return copysign(a, b);
1163#else
1164 int flip = -1;
1165 if ((a <= 0 && b <= 0) || (a > 0 && b > 0)) {
1166 flip = 1;
1167 }
1168 return a * flip;
1169#endif
1170 }
1171
1184 static inline float eman_erfc(float x)
1185 {
1186#ifndef WIN32
1187 return (float)erfc(x);
1188#else
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
1194 };
1195
1196 double result = 1;
1197 double z = fabs(x);
1198 if (z > 0) {
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))));
1203
1204 if (x < 0) {
1205 result = 2 - result;
1206 }
1207 }
1208 return (float)result;
1209#endif
1210 }
1211
1223 static void equation_of_plane(const Vec3f& p1, const Vec3f& p2, const Vec3f& p3, float * plane );
1224
1239 static bool point_is_in_convex_polygon_2d(const Vec2f& p1, const Vec2f& p2, const Vec2f& p3, const Vec2f& p4,const Vec2f& actual_point);
1240
1252 static bool point_is_in_triangle_2d(const Vec2f& p1, const Vec2f& p2, const Vec2f& p3, const Vec2f& actual_point);
1253
1260 static void printMatI3D(MIArray3D& mat,
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);
1271 }
1272
1273// /** Get the isosurface value for 3D image.
1274// *
1275// * @param[in] image 3D image data
1276// * @param[in] surface_value threshhold for isosurface valuse
1277// * @param[in] smooth boolean to specify whether the smooth value needed
1278// *
1279// * @return Dict to wrap the points(float array), and triangles(int array)
1280// *
1281// * @exception ImageDimensionException 3D image only
1282// * */
1283// static Dict get_isosurface(EMData * image, float surface_value, bool smooth);
1284
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);
1297
1304 static bool IsPower2(int x) {
1305 return ( (x>1) && (x & (x-1))==0 );
1306 }
1307
1308
1309 static void apply_precision(float& value, const float& precision) {
1310 float c = ceilf(value);
1311 float f = (float)fast_floor(value);
1312 if (fabs(value - c) < precision) value = c;
1313 else if (fabs(value - f) < precision) value = f;
1314 }
1315 };
1316}
1317
1318#endif
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
Util is a collection of utility functions.
Definition: util.h:90
static double log_2(double x)
Get the log base 2 of a double x.
Definition: util.h:498
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)
Definition: util.cpp:1232
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...
Definition: util.cpp:830
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.
Definition: util.cpp:538
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.
Definition: util.h:724
static float angle3(float x1, float y1, float z1, float x2, float y2, float z2)
Definition: util.h:728
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);.
Definition: util.h:838
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.
Definition: util.h:1084
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);.
Definition: util.h:805
static float angle_norm_pi(float in)
Normalize an angle in radians so it is in the -pi to pi range.
Definition: util.h:1051
static float square(float x)
Calculate a number's square.
Definition: util.h:745
static int MUTEX_UNLOCK(MUTEX *mutex)
Definition: util.cpp:85
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.
Definition: util.h:645
static string remove_filename_ext(const string &filename)
Remove a filename's extension and return the new filename.
Definition: util.cpp:495
static string recv_broadcast(int port)
Definition: util.cpp:1111
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.
Definition: util.cpp:283
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=...
Definition: util.cpp:385
static float agauss(float a, float dx, float dy, float dz, float d)
Calculate Gaussian value.
Definition: util.h:912
static float linear_interpolate(float p1, float p2, float t)
Calculate linear interpolation.
Definition: util.h:518
static T sgn(T &val)
Sign function.
Definition: util.h:1269
static float eman_erfc(float x)
complementary error function.
Definition: util.h:1184
static void flip_complex_phase(float *data, size_t n)
flip the phase of a complex data array.
Definition: util.cpp:127
static std::complex< float > linear_interpolate_cmplx(std::complex< float > p1, std::complex< float > p2, float t)
Calculate linear interpolation.
Definition: util.h:529
static int file_lock_wait(FILE *file)
lock a file.
Definition: util.cpp:182
static int goodf(const double *p_f)
Definition: util.h:1122
static short hypot_fast_int(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
Definition: util.cpp:764
static int MUTEX_LOCK(MUTEX *mutex)
Definition: util.cpp:75
static float fast_acos(const float &f)
Returns an approximate of acos(x) using a cached table and linear interpolation tolerates values very...
Definition: util.cpp:804
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.
Definition: util.h:1066
static vector< EMData * > svdcmp(const vector< EMData * > &data, int nvec)
Perform singular value decomposition on a set of images.
Definition: util.cpp:343
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...
Definition: util.cpp:1304
static int square(int n)
Calculate a number's square.
Definition: util.h:736
static int fast_floor(float x)
A fast way to calculate a floor, which is largest integral value not greater than argument.
Definition: util.h:874
static Dict get_stats(const vector< float > &data)
Get the mean, standard deviation, skewness and kurtosis of the input data.
Definition: util.cpp:913
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...
Definition: util.cpp:577
static void flip_image(float *data, size_t nx, size_t ny)
Vertically flip the data of a 2D real image.
Definition: util.cpp:259
static float square(double x)
Calculate a number's square.
Definition: util.h:754
static float log_2(int i)
Get the log base 2 of an integer i.
Definition: util.h:507
static int goodf(const float *p_f)
Check whether a number is a good float.
Definition: util.h:1112
static float log_2(float x)
Get the log base 2 of a float x.
Definition: util.h:489
static string get_time_label()
Get the current time in a string with format "mm/dd/yyyy hh:mm".
Definition: util.cpp:1167
static float get_min(float f1, float f2, float f3, float f4)
Get the minimum of 4 numbers.
Definition: util.h:978
static bool sstrncmp(const char *s1, const char *s2)
Safe string compare.
Definition: util.cpp:306
static int hypot2sq(int x, int y)
Euclidean distance function squared in 2D: f(x,y) = (x*x + y*y);.
Definition: util.h:784
static float angle_err_ri(float r1, float i1, float r2, float i2)
Calculate the angular phase difference between two r/i vectors.
Definition: util.h:1099
static std::complex< float > gauss3_interpolate_cmplx(std::complex< float > *p, float t, float u)
Calculate 3x3 Gaussian interpolation.
Definition: util.h:589
static float get_max(float f1, float f2, float f3)
Get the maximum of 3 numbers.
Definition: util.h:1009
static float get_min(float f1, float f2)
Get the minimum of 2 numbers.
Definition: util.h:949
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"; ...
Definition: util.cpp:436
static void set_log_level(int argc, char *argv[])
Set program logging level through command line option "-v N", where N is the level.
Definition: util.cpp:1178
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);.
Definition: util.h:816
static float get_max(float f1, float f2, float f3, float f4)
Get the maximum of 4 numbers.
Definition: util.h:1027
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.
Definition: util.cpp:851
static void set_randnum_seed(unsigned long long seed)
Set the seed for Randnum class.
Definition: util.cpp:707
static bool is_nan(const float number)
tell whether a float value is a NaN
Definition: util.h:105
static float get_frand(int low, int high)
Get a float random number between low and high, [low, high)
Definition: util.cpp:725
static string get_filename_ext(const string &filename)
Get a filename's extension.
Definition: util.cpp:526
static float get_min(float f1, float f2, float f3)
Get the minimum of 3 numbers.
Definition: util.h:960
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.
Definition: util.cpp:875
static bool is_file_exist(const string &filename)
check whether a file exists or not
Definition: util.cpp:253
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.
Definition: util.cpp:289
static string sbasename(const string &filename)
Get a filename's basename.
Definition: util.cpp:505
static int get_min(int f1, int f2, int f3)
Get the minimum of 3 numbers.
Definition: util.h:933
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
Definition: util.cpp:140
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).
Definition: util.cpp:1188
static vector< float > nonconvex(const vector< float > &curve, int first=3)
Returns a non-convex version of a curve.
Definition: util.cpp:1037
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);.
Definition: util.h:827
static int round(float x)
Get ceiling round of a float number x.
Definition: util.h:465
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);.
Definition: util.h:849
static void ap2ri(float *data, size_t n)
convert complex data array from Amplitude/Phase format into Real/Imaginary format.
Definition: util.cpp:97
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.
Definition: util.h:573
static int calc_best_fft_size(int low)
Search the best FFT size with good primes.
Definition: util.cpp:1021
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.
Definition: util.cpp:604
static float hypot2sq(float x, float y)
Euclidean distance function squared in 2D: f(x,y) = (x*x + y*y);.
Definition: util.h:794
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.
Definition: util.h:558
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.
Definition: util.h:619
static int get_min(int f1, int f2)
Get the minimum of 2 numbers.
Definition: util.h:922
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
Definition: util.cpp:239
static float bilinear_interpolate(float p1, float p2, float p3, float p4, float t, float u)
Calculate bilinear interpolation.
Definition: util.h:543
static int MUTEX_INIT(MUTEX *mutex)
For those util function developed by Pawel's group.
Definition: util.cpp:64
static string change_filename_ext(const string &old_filename, const string &new_ext)
Change a file's extension and return the new filename.
Definition: util.cpp:485
static void apply_precision(float &value, const float &precision)
Definition: util.h:1309
static string get_line_from_string(char **str)
Extract a single line from a multi-line string.
Definition: util.cpp:322
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.
Definition: util.cpp:1056
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.
Definition: util.cpp:788
static int round(double x)
Get ceiling round of a float number x.
Definition: util.h:477
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,...
Definition: util.cpp:1339
static string int2str(int n)
Get a string format of an integer, e.g.
Definition: util.cpp:315
static float square_sum(float x, float y)
Calcuate (x*x + y*y).
Definition: util.h:764
static void sort_mat(float *left, float *right, int *leftPerm, int *rightPerm)
does a sort as in Matlab.
Definition: util.cpp:670
static int get_irand(int low, int high)
Get an integer random number between low and high, [low, high].
Definition: util.cpp:719
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...
Definition: util.cpp:969
static float angle_norm_2pi(float in)
Normalize an angle in radians so it is in the 0-2pi range.
Definition: util.h:1043
static unsigned long long get_randnum_seed()
Get the seed for Randnum class.
Definition: util.cpp:713
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.
Definition: util.cpp:1293
static float get_gauss_rand(float mean, float sigma)
Get a Gaussian random number.
Definition: util.cpp:845
static float eman_copysign(float a, float b)
copy sign of a number.
Definition: util.h:1159
static float get_max(float f1, float f2)
Get the maximum of 2 numbers.
Definition: util.h:998
static float hypot_fast(int x, int y)
Euclidean distance in 2D for integers computed fast using a cached lookup table.
Definition: util.cpp:742
static float hypot2(float x, float y)
Euclidean distance function in 2D: f(x,y) = sqrt(x*x + y*y);.
Definition: util.h:774
static bool IsPower2(int x)
Return true if an integer is positive and is power of 2.
Definition: util.h:1304
The Vec2 is precisely the same as Vec3 except it works exclusively in 2D Note there are convenient ty...
Definition: vec3.h:710
EMData * log() const
return natural logarithm image for a image
EMData * sqrt() const
return square root of current image
E2Exception class.
Definition: aligner.h:40
boost::multi_array< int, 3 > MIArray3D
Definition: emdata.h:73
#define y(i, j)
Definition: projector.cpp:1516
#define x(i)
Definition: projector.cpp:1517
#define MUTEX
Definition: util.h:43
const int good_fft_sizes[]
Definition: util.h:64