GCOP  1.0
utils.h
Go to the documentation of this file.
00001 #ifndef GCOP_UTILS_H
00002 #define GCOP_UTILS_H
00003 
00004 #include <cstdlib>
00005 #include <cmath>
00006 #include <sys/time.h>
00007 #include <cstring>
00008 #include <iostream>
00009 #include <stdio.h>
00010 
00011 namespace gcop {
00012 
00013 #if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
00014 #define OS_WIN
00015 #include <windows.h>
00016 #endif
00017 
00018 #ifdef OS_WIN  
00019 
00020 #ifndef usleep
00021 #define usleep(x) Sleep(x/1000)
00022 #endif
00023 
00024 #endif
00025 
00026 #define EPS (1e-10)
00027 #define ISZERO(x) (fabs(x)<EPS)
00028 
00029 #ifndef MAX
00030 #define MAX(a,b)((a)>(b)?(a):(b))
00031 #endif
00032 #ifndef MIN
00033 #define MIN(a,b)((a)<(b)?(a):(b))
00034 #endif
00035 
00036 #ifndef RND
00037 #define RND (std::rand()/(double)RAND_MAX)
00038 #endif
00039 
00040 #define TIME_CMP(a,b) (a.tv_sec != b.tv_sec || a.tv_usec != b.tv_usec)
00041 
00042 #ifndef RAD2DEG
00043 #define RAD2DEG(x) (180.0*(x)/M_PI)
00044 #endif
00045 #ifndef DEG2RAD
00046 #define DEG2RAD(x) (M_PI*(x)/180)
00047 #endif
00048 
00049 #define SQR(a) ((a)*(a))
00050 
00051 #define SIGN(a) ((a) < 0 ? -1 : 1)
00052 
00053 //#define DIST(ya,ya,xb,yb) (sqrt(((xa)-(xb))*((xa)-(xb))+((ya)-(yb))*((ya)-(yb))))
00054 //#define NORM(x,y) (sqrt((x)*(x)+(y)*(y)))
00055 
00056 #define DIST(a,b) (sqrt(((a)[0]-(b)[0])*((a)[0]-(b)[0])+((a)[1]-(b)[1])*((a)[1]-(b)[1])))
00057 #define NORM(a) (sqrt((a)[0]*(a)[0]+(a)[1]*(a)[1]))
00058 #define MINUS(c,a,b) (c)[0]=(a)[0]-(b)[0];(c)[1]=(a)[1]-(b)[1];
00059 #define PLUS(c,a,b) (c)[0]=(a)[0]+(b)[0];(c)[1]=(a)[1]+(b)[1];
00060 #define DOT(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1])
00061 #define MULT(a,b,n) (a)[0]=(b)[0]*(n);(a)[1]=(b)[1]*(n);
00062 #define DIV(a,b,n) (a)[0]=(b)[0]/(n);(a)[1]=(b)[1]/(n);
00063 #define DET(a,b) ((a)[0]*(b)[1]-(a)[1]*(b)[0])
00064 #define NEG(a,b) (a)[0]=-(b)[0];(a)[1]=-(b)[1];
00065 
00066 #define SET(a,b) (a)[0]=(b)[0];(a)[1]=(b)[1];
00067 #define CLEAR(a) (a)[0]=0;(a)[1]=0;
00068 
00069 #define DIST3(a,b) (sqrt(SQR((a)[0]-(b)[0])+SQR((a)[1]-(b)[1])+SQR((a)[2]-(b)[2])))
00070 #define NORM3(a) (sqrt((a)[0]*(a)[0]+(a)[1]*(a)[1]+(a)[2]*(a)[2]))
00071 #define MINUS3(c,a,b) (c)[0]=(a)[0]-(b)[0];(c)[1]=(a)[1]-(b)[1];(c)[2]=(a)[2]-(b)[2];
00072 #define PLUS3(c,a,b) (c)[0]=(a)[0]+(b)[0];(c)[1]=(a)[1]+(b)[1];(c)[2]=(a)[2]+(b)[2];
00073 #define AVE3(c,a,b) (c)[0]=((a)[0]+(b)[0])/2;(c)[1]=((a)[1]+(b)[1])/2;(c)[2]=((a)[2]+(b)[2])/2;
00074 #define DOT3(a,b) ((a)[0]*(b)[0]+(a)[1]*(b)[1]+(a)[2]*(b)[2])
00075 #define MULT3(a,b,n) (a)[0]=(b)[0]*(n);(a)[1]=(b)[1]*(n);(a)[2]=(b)[2]*(n);
00076 #define DIV3(a,b,n) (a)[0]=(b)[0]/(n);(a)[1]=(b)[1]/(n);(a)[2]=(b)[2]/(n);
00077 //#define DET3(a,b) ((a)[0]*(b)[1]-(a)[1]*(b)[0])
00078 #define NEG3(a,b) (a)[0]=-(b)[0];(a)[1]=-(b)[1];(a)[2]=-(b)[2];
00079 
00080 #define SET3(a,b) (a)[0]=(b)[0];(a)[1]=(b)[1];(a)[2]=(b)[2];
00081 #define CLEAR3(a) (a)[0]=0;(a)[1]=0;(a)[2]=0;
00082 
00083 #define CROSS(c,a,b) (c)[0]=(a)[1]*(b)[2]-(a)[2]*(b)[1]; (c)[1]=-(a)[0]*(b)[2]+(a)[2]*(b)[0]; (c)[2]=(a)[0]*(b)[1]-(a)[1]*(b)[0]; 
00084 
00085 #define NORM4(a) (sqrt((a)[0]*(a)[0]+(a)[1]*(a)[1]+(a)[2]*(a)[2]+(a)[3]*(a)[3]))
00086 #define MINUS4(c,a,b) (c)[0]=(a)[0]-(b)[0];(c)[1]=(a)[1]-(b)[1];(c)[2]=(a)[2]-(b)[2];(c)[3]=(a)[3]-(b)[3];
00087 #define PLUS4(c,a,b) (c)[0]=(a)[0]+(b)[0];(c)[1]=(a)[1]+(b)[1];(c)[2]=(a)[2]+(b)[2];(c)[3]=(a)[3]+(b)[3];
00088 #define MULT4(a,b,n) (a)[0]=(b)[0]*(n);(a)[1]=(b)[1]*(n);(a)[2]=(b)[2]*(n);(a)[3]=(b)[3]*(n);
00089 #define DIV4(a,b,n) (a)[0]=(b)[0]/(n);(a)[1]=(b)[1]/(n);(a)[2]=(b)[2]/(n);(a)[3]=(b)[3]/(n);
00090 #define SET4(a,b) (a)[0]=(b)[0];(a)[1]=(b)[1];(a)[2]=(b)[2];(a)[3]=(b)[3];
00091 #define CLEAR4(a) (a)[0]=0;(a)[1]=0;(a)[2]=0;(a)[3]=0;
00092 #define DIST4(a,b) (sqrt(SQR((a)[0]-(b)[0])+SQR((a)[1]-(b)[1])+SQR((a)[2]-(b)[2])+SQR((a)[3]-(b)[3])))
00093 
00098 inline void timer_start(struct timeval &timer)
00099 {
00100   gettimeofday(&timer, 0);
00101 }
00102 
00109 inline long timer_us(struct timeval &timer)
00110 {
00111   struct timeval now;
00112   gettimeofday(&now, 0);
00113   return (now.tv_sec - timer.tv_sec)*1000000 + now.tv_usec - timer.tv_usec;
00114 }
00115 
00123 inline double normal_dist(double mu, double sigma, double x) 
00124 {
00125   return exp(-(x - mu)*(x - mu)/(2*sigma*sigma))/(sigma*sqrt(2*M_PI));
00126 }
00127 
00132 inline double random_normal()
00133 {
00134   static const double r_max=RAND_MAX;
00135   double U1,U2,V1,V2;
00136   double S = 2;
00137   while(S>=1) {
00138     U1 = std::rand()/r_max;
00139     U2 = std::rand()/r_max;
00140     V1 = 2*U1-1;
00141     V2 = 2*U2-1;
00142     S = V1*V1+V2*V2;
00143   }
00144   return V1*sqrt(-2*log(S)/S);
00145 }
00146 
00147 
00148 
00159  inline void mult(double *c, const double *a, const double *b, int m, int n, int p, bool rm = true)
00160 {
00161   memset(c, 0, m*p*sizeof(double));
00162   
00163   if (rm)
00164     for (int i=0; i<m; ++i) {
00165       for (int k=0; k<p; ++k) {
00166         double& cr = c[i*p+k];
00167         for (int j=0; j<n; ++j)
00168           cr += a[i*n+j]*b[j*p+k];
00169       }
00170     }
00171   else
00172     for (int i=0; i<m; ++i) {
00173       for (int k=0; k<p; ++k) {
00174         double& cr = c[i+k*m];
00175         for (int j=0; j<n; ++j)
00176           cr += a[i+j*m]*b[j+k*n];       
00177       }
00178     }
00179 }
00180  
00194 void mult(double *c, 
00195           const double *a, int an, const int *ai, const int *aj, 
00196           const double *b, 
00197           int m, int n, int p);
00198 
00205 double dangle(double a1, double a2);
00206 
00214 void save_map(const char* map, int width, int height, const char* filename);
00215 
00223 char* load_map(int* width, int* height, const char* filename);
00224 
00225 
00226 void rot(double m[9], const double o[3]);
00227 
00238 void hermite2(double c1[2], double c2[2], double c3[2], 
00239               const double xi[2], const double vi[2],
00240               const double xf[2]);
00241 
00242 
00253 void hermite3(double c1[3], double c2[3], double c3[3], 
00254               const double xi[3], const double vi[3],
00255               const double xf[3]);
00256 
00257 
00258 
00266  double normal(double mu, double sigma, double x);
00267 
00276  double normal(int d, const double *mu, const double *sigma, const double *x);
00277 
00286  double normal2(const double mu[2], const double sigma[2], const double x[2]);
00287 
00292  double randn();
00293 
00299 double ncdf(double x);
00300 
00301 }
00302 
00303 #endif