util.h

Go to the documentation of this file.
00001 
00005 /*
00006  * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
00007  * Copyright (c) 2000-2006 Baylor College of Medicine
00008  *
00009  * This software is issued under a joint BSD/GNU license. You may use the
00010  * source code in this file under either license. However, note that the
00011  * complete EMAN2 and SPARX software packages have some GPL dependencies,
00012  * so you are responsible for compliance with the licenses of these packages
00013  * if you opt to use BSD licensing. The warranty disclaimer below holds
00014  * in either instance.
00015  *
00016  * This complete copyright notice must be included in any revised version of the
00017  * source code. Additional authorship citations may be added, but existing
00018  * author citations must be preserved.
00019  *
00020  * This program is free software; you can redistribute it and/or modify
00021  * it under the terms of the GNU General Public License as published by
00022  * the Free Software Foundation; either version 2 of the License, or
00023  * (at your option) any later version.
00024  *
00025  * This program is distributed in the hope that it will be useful,
00026  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00027  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00028  * GNU General Public License for more details.
00029  *
00030  * You should have received a copy of the GNU General Public License
00031  * along with this program; if not, write to the Free Software
00032  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00033  *
00034  * */
00035 
00036 #ifndef eman__util_h__
00037 #define eman__util_h__ 1
00038 
00039 #ifdef _WIN32
00040         #pragma warning(disable:4819)
00041 #endif  //_WIN32
00042 
00043 #include "sparx/emconstants.h"
00044 #include "exception.h"
00045 #include <vector>
00046 #include <iostream>
00047 
00048 #include <string>
00049 using std::string;
00050 
00051 
00052 #include <boost/multi_array.hpp>
00053 #include <boost/tuple/tuple.hpp>
00054 //#include <boost/math/special_functions/fpclassify.hpp>
00055 #include "vec3.h"
00056 
00057 #ifdef WIN32
00058 #include <windows.h>
00059 #define M_PI 3.14159265358979323846f
00060 //#define MAXPATHLEN (MAX_PATH * 4)
00061 #endif
00062 
00063 using std::string;
00064 using std::vector;
00065 using std::ostream;
00066 using std::cout;
00067 using std::endl;
00068 
00069 
00070 namespace EMAN
00071 {
00072         class EMData;
00073         class Dict;
00074 
00075         typedef boost::multi_array<int, 3> MIArray3D;
00076 
00080         class Util
00081         {
00083                 #include "sparx/util_sparx.h"
00084 
00085           public:
00091                 static void ap2ri(float *data, size_t n);
00092 
00097                 static void flip_complex_phase(float *data, size_t n);
00098 
00106                 static void rotate_phase_origin(float *data, size_t nx, size_t ny, size_t nz);
00107 
00115                 static int file_lock_wait(FILE * file);
00116                 //static void file_unlock(FILE * file);
00117 
00123                 static bool check_file_by_magic(const void *first_block, const char *magic);
00124 
00128                 static bool is_file_exist(const string & filename);
00129 
00135                 static void flip_image(float *data, size_t nx, size_t ny);
00136 
00142                 static vector<EMData *> svdcmp(const vector<EMData *> &data,int nvec);
00143 
00144 
00149                 static string str_to_lower(const string& s);
00150 
00159                 static bool sstrncmp(const char *s1, const char *s2);
00160 
00165                 static string int2str(int n);
00166 
00175                 static string get_line_from_string(char **str);
00176 
00188                 static bool get_str_float(const char *str, const char *float_var, float *p_val);
00189 
00190 
00203                 static bool get_str_float(const char *str, const char *float_var,
00204                                                                   float *p_v1, float *p_v2);
00205 
00224                 static bool get_str_float(const char *str, const char *float_var,
00225                                                                   int *p_nvalues, float *p_v1, float *p_v2);
00226 
00238                 static bool get_str_int(const char *str, const char *int_var, int *p_val);
00239 
00252                 static bool get_str_int(const char *str, const char *int_var,
00253                                                                 int *p_v1, int *p_v2);
00254 
00273                 static bool get_str_int(const char *str, const char *int_var,
00274                                                                 int *p_nvalues, int *p_v1, int *p_v2);
00275 
00286                 static string change_filename_ext(const string& old_filename,
00287                                                                                   const string & new_ext);
00288 
00295                 static string remove_filename_ext(const string& filename);
00296 
00302                 static string get_filename_ext(const string& filename);
00303 
00310                 static string sbasename(const string & filename);
00311 
00324                 static void calc_least_square_fit(size_t nitems, const float *data_x,
00325                                                                                   const float *data_y, float *p_slope,
00326                                                                                   float *p_intercept, bool ignore_zero,float absmax=0);
00327 
00334                 static Vec3f calc_bilinear_least_square(const vector<float> &points);
00335 
00346                 static void save_data(const vector < float >&x_array,
00347                                                           const vector < float >&y_array,
00348                                                           const string & filename);
00349 
00358                 static void save_data(float x0, float dx,
00359                                                           const vector < float >&y_array,
00360                                                           const string & filename);
00361 
00371                 static void save_data(float x0, float dx, float *y_array,
00372                                                           size_t array_size, const string & filename);
00373 
00374 
00383                 static void sort_mat(float *left, float *right, int *leftPerm,
00384                                                          int *rightPerm);
00385 
00386 
00390                 static unsigned long int get_randnum_seed();
00391 
00395                 static void set_randnum_seed(unsigned long int seed);
00396 
00402                 static int get_irand(int low, int high);
00403 
00409                 static float get_frand(int low, int high);
00410 
00416                 static float get_frand(float low, float high);
00417 
00423                 static float get_frand(double low, double high);
00424 
00431                 static float get_gauss_rand(float mean, float sigma);
00432 
00437                 static inline int round(float x)
00438                 {
00439                         if (x < 0) {
00440                                 return (int) (x - 0.5f);
00441                         }
00442                         return (int) (x + 0.5f);
00443                 }
00444 
00449                 static inline int round(double x)
00450                 {
00451                         if (x < 0) {
00452                                 return (int) (x - 0.5);
00453                         }
00454                         return (int) (x + 0.5);
00455                 }
00456 
00463                 static inline float linear_interpolate(float p1, float p2, float t)
00464                 {
00465                         return (1-t) * p1 + t  * p2;
00466                 }
00467 
00477                 static inline float bilinear_interpolate(float p1, float p2, float p3,
00478                                                                                                  float p4, float t, float u)
00479                 {
00480                         return (1-t) * (1-u) * p1 + t * (1-u) * p2 + (1-t) * u * p3 + t * u * p4;
00481                 }
00482 
00498                 static inline float trilinear_interpolate(float p1, float p2, float p3,
00499                                                           float p4, float p5, float p6,
00500                                                          float p7, float p8, float t,
00501                                                                   float u, float v)
00502                 {
00503                         return ((1 - t) * (1 - u) * (1 - v) * p1 + t * (1 - u) * (1 - v) * p2
00504                                         + (1 - t) * u * (1 - v) * p3 + t * u * (1 - v) * p4
00505                                         + (1 - t) * (1 - u) * v * p5 + t * (1 - u) * v * p6
00506                                         + (1 - t) * u * v * p7 + t * u * v * p8);
00507                 }
00508 
00515                 static void find_max(const float *data, size_t nitems,
00516                                                          float *p_max_val, int *p_max_index = 0);
00517 
00528                 static void find_min_and_max(const float *data, size_t nitems,
00529                                                                          float *p_max_val, float *p_min_val,
00530                                                                          int *p_max_index = 0, int *p_min_index = 0);
00531 
00532 
00537                 static Dict get_stats( const vector<float>& data );
00538                 static Dict get_stats_cstyle( const vector<float>& data );
00539 
00546                 static int calc_best_fft_size(int low);
00547 
00548 
00549                 static EMData* calc_bessel(const int n, const float& x);
00550 
00555                 static inline int square(int n)
00556                 {
00557                         return (n * n);
00558                 }
00559 
00564                 static inline float square(float x)
00565                 {
00566                         return (x * x);
00567                 }
00568 
00573                 static inline float square(double x)
00574                 {
00575                         return (float)(x * x);
00576                 }
00577 
00583                 static inline float square_sum(float x, float y)
00584                 {
00585                         return (float)(x * x + y * y);
00586                 }
00587 
00593                 static inline float hypot2(float x, float y)
00594                 {
00595                         return sqrtf(x * x + y * y);
00596                 }
00597 
00604                 static inline float hypot3(int x, int y, int z)
00605                 {
00606                         return sqrtf((float)(x * x + y * y + z * z));
00607                 }
00608 
00615                 static inline float hypot3(float x, float y, float z)
00616                 {
00617                         return sqrtf(x * x + y * y + z * z);
00618                 }
00619 
00626                 static inline float hypot3(double x, double y, double z)
00627                 {
00628                         return (float) sqrt(x * x + y * y + z * z);
00629                 }
00630 
00636                 static float hypot_fast(int x, int y); 
00637 
00643                 static short hypot_fast_int(int x, int y); 
00644 
00651                 static inline int fast_floor(float x)
00652                 {
00653                         if (x < 0) {
00654                                 return ((int) x - 1);
00655                         }
00656                         return (int) x;
00657                 }
00658 
00667                 static inline float agauss(float a, float dx, float dy, float dz, float d)
00668                 {
00669                         return (a * exp(-(dx * dx + dy * dy + dz * dz) / d));
00670                 }
00671 
00677                 static inline int get_min(int f1, int f2)
00678                 {
00679                         return (f1 < f2 ? f1 : f2);
00680                 }
00681 
00688                 static inline int get_min(int f1, int f2, int f3)
00689                 {
00690                         if (f1 <= f2 && f1 <= f3) {
00691                                 return f1;
00692                         }
00693                         if (f2 <= f1 && f2 <= f3) {
00694                                 return f2;
00695                         }
00696                         return f3;
00697                 }
00698 
00704                 static inline float get_min(float f1, float f2)
00705                 {
00706                         return (f1 < f2 ? f1 : f2);
00707                 }
00708 
00715                 static inline float get_min(float f1, float f2, float f3)
00716                 {
00717                         if (f1 <= f2 && f1 <= f3) {
00718                                 return f1;
00719                         }
00720                         if (f2 <= f1 && f2 <= f3) {
00721                                 return f2;
00722                         }
00723                         return f3;
00724                 }
00725 
00726 
00734                 static inline float get_min(float f1, float f2, float f3, float f4)
00735                 {
00736                         float m = f1;
00737                         if (f2 < m) {
00738                                 m = f2;
00739                         }
00740                         if (f3 < m) {
00741                                 m = f3;
00742                         }
00743                         if (f4 < m) {
00744                                 m = f4;
00745                         }
00746                         return m;
00747                 }
00748 
00754                 static inline float get_max(float f1, float f2)
00755                 {
00756                         return (f1 < f2 ? f2 : f1);
00757                 }
00758 
00765                 static inline float get_max(float f1, float f2, float f3)
00766                 {
00767                         if (f1 >= f2 && f1 >= f3) {
00768                                 return f1;
00769                         }
00770                         if (f2 >= f1 && f2 >= f3) {
00771                                 return f2;
00772                         }
00773                         return f3;
00774                 }
00775 
00783                 static inline float get_max(float f1, float f2, float f3, float f4)
00784                 {
00785                         float m = f1;
00786                         if (f2 > m) {
00787                                 m = f2;
00788                         }
00789                         if (f3 > m) {
00790                                 m = f3;
00791                         }
00792                         if (f4 > m) {
00793                                 m = f4;
00794                         }
00795                         return m;
00796                 }
00797 
00805                 static inline float angle_sub_2pi(float x, float y)
00806                 {
00807                         float r = fmod(fabs(x - y), (float) (2 * M_PI));
00808                         if (r > M_PI) {
00809                                 r = (float) (2.0 * M_PI - r);
00810                         }
00811 
00812                         return r;
00813                 }
00814 
00822                 static inline float angle_sub_pi(float x, float y)
00823                 {
00824                         float r = fmod(fabs(x - y), (float) M_PI);
00825                         if (r > M_PI / 2.0) {
00826                                 r = (float)(M_PI - r);
00827                         }
00828                         return r;
00829                 }
00830 
00837                 static inline int goodf(const float *p_f)
00838                 {
00839                         //This is the old way to judge a good float, which cause problem on
00840                         //Fedora Core 64 bit system. Now use isinff() and isnanf() on Linux.
00841                         //#if defined(_WIN32) || defined(__APPLE__)
00842                         #if defined(_WIN32)
00843                         // the first is abnormal zero the second is +-inf or NaN
00844                         if ((((int *) p_f)[0] & 0x7f800000) == 0 ||
00845                                 (((int *) p_f)[0] & 0x7f800000) == 255) {
00846                                 return 0;
00847                         }
00848                         #else
00849                         using std::fpclassify;
00850                         int i = fpclassify(*p_f);
00851                         if ( i == FP_NAN || i == FP_INFINITE) return 0;
00852                         #endif  //_WIN32 || __APPLE__
00853                         
00854                         return 1;
00855                 }
00856                 
00857                 static inline int goodf(const double *p_f)
00858                 {
00859                         //This is the old way to judge a good float, which cause problem on
00860                         //Fedora Core 64 bit system. Now use isinff() and isnanf() on Linux.
00861                         //#if defined(_WIN32) || defined(__APPLE__)
00862                         #if defined(_WIN32)
00863                         // the first is abnormal zero the second is +-inf or NaN
00864                         if ((((int *) p_f)[0] & 0x7f800000) == 0 ||
00865                                 (((int *) p_f)[0] & 0x7f800000) == 255) {
00866                                 return 0;
00867                         }
00868                         #else
00869                         using std::fpclassify;
00870                         int i = fpclassify(*p_f);
00871                         if ( i == FP_NAN || i == FP_INFINITE) return 0;
00872                         #endif  //_WIN32 || __APPLE__
00873                         
00874                         return 1;
00875                 }
00876 
00880                 static string get_time_label();
00881 
00888                 static void set_log_level(int argc, char *argv[]);
00889 
00900                 static inline float eman_copysign(float a, float b)
00901                 {
00902 #ifndef WIN32
00903                         return copysign(a, b);
00904 #else
00905                         int flip = -1;
00906                         if ((a <= 0 && b <= 0) || (a > 0 && b > 0)) {
00907                                 flip = 1;
00908                         }
00909                         return a * flip;
00910 #endif
00911                 }
00912 
00925                 static inline float eman_erfc(float x)
00926                 {
00927 #ifndef WIN32
00928                         return (float)erfc(x);
00929 #else
00930                         static double a[] = { -1.26551223, 1.00002368,
00931                                                                   0.37409196, 0.09678418,
00932                                                                   -0.18628806, 0.27886807,
00933                                                                   -1.13520398, 1.48851587,
00934                                                                   -0.82215223, 0.17087277
00935                         };
00936 
00937                         double result = 1;
00938                         double z = fabs(x);
00939                         if (z > 0) {
00940                                 double t = 1 / (1 + 0.5 * z);
00941                                 double f1 = t * (a[4] + t * (a[5] + t * (a[6] +
00942                                                         t * (a[7] + t * (a[8] + t * a[9])))));
00943                                 result = t * exp((-z * z) + a[0] + t * (a[1] + t * (a[2] + t * (a[3] + f1))));
00944 
00945                                 if (x < 0) {
00946                                         result = 2 - result;
00947                                 }
00948                         }
00949                         return (float)result;
00950 #endif
00951                 }
00952 
00964                 static void equation_of_plane(const Vec3f& p1, const Vec3f& p2, const Vec3f& p3, float * plane );
00965 
00966 
00967 
00982                 static bool point_is_in_convex_polygon_2d(const Vec2f& p1, const Vec2f& p2, const Vec2f& p3, const Vec2f& p4,const Vec2f& actual_point);
00983 
00995                 static bool point_is_in_triangle_2d(const Vec2f& p1, const Vec2f& p2, const Vec2f& p3, const Vec2f& actual_point);
00996 
01003                 static void printMatI3D(MIArray3D& mat,
01004                                                                 const string str = string(""),
01005                                                                 ostream& out = std::cout);
01012                 template<class T> static inline T sgn(T& val) {
01013                         return (val > 0) ? T(+1) : T(-1);
01014                 }
01015 
01016 //              /** Get the isosurface value for 3D image.
01017 //               *
01018 //               * @param[in] image 3D image data
01019 //               * @param[in] surface_value threshhold for isosurface valuse
01020 //               * @param[in] smooth boolean to specify whether the smooth value needed
01021 //               *
01022 //               * @return Dict to wrap the points(float array), and triangles(int array)
01023 //               *
01024 //               * @exception ImageDimensionException 3D image only
01025 //               * */
01026 //              static Dict get_isosurface(EMData * image, float surface_value, bool smooth);
01027 
01039                 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);
01040 
01047                 static bool IsPower2(int x) {
01048                         return ( (x>1) && (x & (x-1))==0 );
01049                 }
01050 
01051 
01052                 static void apply_precision(float& value, const float& precision) {
01053                         float c = ceilf(value);
01054                         float f = (float)fast_floor(value);
01055                         if (fabs(value - c) < precision) value = c;
01056                         else if (fabs(value - f) < precision) value = f;
01057                 }
01058         };
01059 }
01060 
01061 #endif
01062 
01063 /* vim: set ts=4 noet nospell: */

Generated on Sat Nov 7 02:18:57 2009 for EMAN2 by  doxygen 1.5.6