Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

utilities.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr                                                                       
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the           
00004  *cr                        University of Illinois                       
00005  *cr                         All Rights Reserved                        
00006  *cr                                                                   
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: utilities.h,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.87 $       $Date: 2008/08/13 22:23:32 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *
00019  * General utility routines and definitions.
00020  *
00021  ***************************************************************************/
00022 #ifndef UTILITIES_H
00023 #define UTILITIES_H
00024 
00025 #ifndef FALSE
00026 #define FALSE 0
00027 #define TRUE  1
00028 #endif
00029 
00030 #ifndef NULL
00031 #define NULL 0
00032 #endif
00033 
00034 #ifndef ABS
00035 #define ABS(A) ((A)>0?(A):-(A))
00036 #endif
00037 
00038 // various PI macros not available on all machines
00039 #define VMD_PI      3.14159265358979323846
00040 #define VMD_TWOPI   (2.0 * VMD_PI)
00041 #define VMD_1_PI    0.31830988618379067154
00042 
00043 // Degree-to-Radians and Radians-to-Degrees Conversion macros
00044 #define DEGTORAD(a)     (a*VMD_PI/180.0)
00045 #define RADTODEG(a)     (a*180.0/VMD_PI)
00046 
00050 extern char *combine_arguments(int, const char **, int);
00051 
00052 
00054 extern char *stringdup(const char *);
00055 
00056 
00058 extern char *stringtoupper(char *);
00059 
00061 void stripslashes(char *str);
00062 
00064 extern int strupcmp(const char *, const char *);
00066 extern int strupncmp(const char *, const char *, int);
00067 
00068 
00072 extern void breakup_filename(const char *, char **, char **);
00073 
00075 extern char *str_tokenize(const char *, int *, char **);
00076 
00079 extern double time_of_day(void);
00080 
00081 typedef void * vmd_timerhandle;            
00082 vmd_timerhandle vmd_timer_create(void);    
00083 void vmd_timer_destroy(vmd_timerhandle);   
00084 void vmd_timer_start(vmd_timerhandle);     
00085 void vmd_timer_stop(vmd_timerhandle);      
00086 double vmd_timer_time(vmd_timerhandle);    
00087 double vmd_timer_timenow(vmd_timerhandle); 
00088 
00089 typedef struct {
00090   vmd_timerhandle timer;
00091   double updatetime;
00092 } msgtimer;
00093 
00095 extern msgtimer * msg_timer_create(double updatetime);
00096 
00098 extern int msg_timer_timeout(msgtimer *time);
00099 
00101 void msg_timer_destroy(msgtimer * mt);
00102 
00104 extern int vmd_check_stdin(void);
00105 
00107 extern char * vmd_username(void);
00109 extern int vmd_getuid(void);
00110 
00112 inline int clamp_int(int val, int min, int max) {
00113   return   (val > min) ? ((val <= max) ? val : max) : 0;
00114 }
00115 
00118 extern float * cross_prod(float *x1, const float *x2, const float *x3);
00119 
00121 inline float dot_prod(const float *v1, const float *v2) {
00122   return v1[0]* v2[0] + v1[1]* v2[1] + v1[2] * v2[2];
00123 }
00124 
00125 inline double dot_prod(const double *v1, const double *v2) {
00126   return v1[0]* v2[0] + v1[1]* v2[1] + v1[2] * v2[2];
00127 }
00128 
00130 inline void vec_copy(float *v1, const float *v2) {
00131   v1[0] = v2[0];
00132   v1[1] = v2[1];
00133   v1[2] = v2[2];
00134 }
00135 
00138 extern float * vec_normalize(float *);
00139 
00142 inline void vec_sub(float *a, const float *b, const float *c) {
00143   a[0]=b[0]-c[0];
00144   a[1]=b[1]-c[1];
00145   a[2]=b[2]-c[2];
00146 }
00147 
00149 inline void vec_add(float *a, const float *b, const float *c) {
00150   a[0]=b[0]+c[0];
00151   a[1]=b[1]+c[1];
00152   a[2]=b[2]+c[2];
00153 }
00154 
00156 inline void vec_incr(float *a, const float *b) {
00157   a[0] += b[0];
00158   a[1] += b[1];
00159   a[2] += b[2];
00160 }
00161 
00163 inline void vec_scale(float *a, float b, const float *c) {
00164   a[0] = b*c[0];
00165   a[1] = b*c[1];
00166   a[2] = b*c[2];
00167 }
00168 
00170 inline void vec_scale(float *a, float b, const double *c) {
00171   a[0] = b * (float) c[0];
00172   a[1] = b * (float) c[1];
00173   a[2] = b * (float) c[2];
00174 }
00175 
00177 inline void vec_negate(float *a, const float *b) {
00178   a[0] = -b[0];
00179   a[1] = -b[1];
00180   a[2] = -b[2];
00181 }
00182 
00184 inline void vec_scaled_add(float *a, float b, const float *c) {
00185   a[0] += b*c[0];
00186   a[1] += b*c[1];
00187   a[2] += b*c[2];
00188 }
00189 
00191 inline void vec_triad(float *a, const float *b, float c, const float *d) {
00192   a[0] = b[0] + c*d[0];
00193   a[1] = b[1] + c*d[1];
00194   a[2] = b[2] + c*d[2];
00195 }
00196 
00198 inline void vec_lerp(float *a, const float *b, const float *c, float frac) {
00199   float diff[3], tmp[3];
00200   vec_sub(diff, c, b);
00201   vec_scale(tmp, frac, diff);
00202   vec_add(a, b, tmp);
00203 }
00204 
00205 inline void vec_zero(float *a) {
00206   a[0] = 0.0f;
00207   a[1] = 0.0f;
00208   a[2] = 0.0f;
00209 }
00210 
00211 inline void clamp_color(float *rgb) {
00212   // clamp color values to legal range
00213   if (rgb[0] < 0.0f)
00214     rgb[0] = 0.0f;
00215   if (rgb[0] > 1.0f)
00216     rgb[0] = 1.0f;
00217 
00218   if (rgb[1] < 0.0f)
00219     rgb[1] = 0.0f;
00220   if (rgb[1] > 1.0f)
00221     rgb[1] = 1.0f;
00222 
00223   if (rgb[1] < 0.0f)
00224     rgb[1] = 0.0f;
00225   if (rgb[1] > 1.0f)
00226     rgb[1] = 1.0f;
00227 }
00228 
00229 
00231 inline void midpoint(float *a, const float *b, const float *c) {
00232   a[0] = 0.5f * (b[0]+c[0]);
00233   a[1] = 0.5f * (b[1]+c[1]);
00234   a[2] = 0.5f * (b[2]+c[2]);
00235 }
00236 
00237 
00238 // These define a cubic spline with various bases
00239 // see Foley and Van Dam, et. al., Computer Graphics, p505 or so
00240 //
00241 // this one was too sharply curved for my tastes
00242 //
00243 // float CatmullRom_basis[4][4]={{-1.0/2.0,  3.0/2.0, -3.0/2.0,  1.0/2.0},
00244 //                               { 2.0/2.0, -5.0/2.0,  4.0/2.0, -1.0/2.0},
00245 //                               {-1.0/2.0,  0.0/2.0,  1.0/2.0,  0.0/2.0},
00246 //                               { 0.0/2.0,  2.0/2.0,  0.0/2.0,  0.0/2.0}};
00247 //
00248 // this define makes the next basis identical to CatmullRom
00249 // #define SLOPE 2.0f
00250 //
00251 // This deemphasizes the slope and makes things look nicer (IMHO)
00252 // #define SLOPE 1.25f
00253 //
00254 // float modified_CR_basis[4][4] = {
00255 //    {-1.0f/SLOPE,  -1.0f/SLOPE + 2.0f,  1.0f/SLOPE - 2.0f,  1.0f/SLOPE},
00256 //    { 2.0f/SLOPE,   1.0f/SLOPE - 3.0f,   -2.0f/SLOPE+3.0f, -1.0f/SLOPE},
00257 //    {-1.0f/SLOPE,                0.0f,         1.0f/SLOPE,        0.0f},
00258 //    {       0.0f,                1.0f,               0.0f,        0.0f}
00259 // };
00260 //
00261 // This doesn't describe the system very nicely as the lines don't
00262 // go through the control points (which are the C-alphas)
00263 // float Bspline_basis[4][4]={{-1.0/6.0,  3.0/6.0, -3.0/6.0,  1.0/6.0},
00264 //                            { 3.0/6.0, -6.0/6.0,  3.0/6.0,  0.0/6.0},
00265 //                            {-3.0/6.0,  0.0/6.0,  3.0/6.0,  0.0/6.0},
00266 //                            { 1.0/6.0,  4.0/6.0,  1.0/6.0,  0.0/6.0}};
00267 
00269 inline void create_Bspline_basis(float array[4][4]) {
00270   array[0][0] = -1.0f/6.0f;
00271   array[0][1] =  3.0f/6.0f;
00272   array[0][2] = -3.0f/6.0f;
00273   array[0][3] =  1.0f/6.0f;
00274   array[1][0] =  3.0f/6.0f;
00275   array[1][1] = -6.0f/6.0f;
00276   array[1][2] =  3.0f/6.0f;
00277   array[1][3] =  0.0f/6.0f;
00278   array[2][0] = -3.0f/6.0f;
00279   array[2][1] =  0.0f/6.0f;
00280   array[2][2] =  3.0f/6.0f;
00281   array[2][3] =  0.0f/6.0f;
00282   array[3][0] =  1.0f/6.0f;
00283   array[3][1] =  4.0f/6.0f;
00284   array[3][2] =  1.0f/6.0f;
00285   array[3][3] =  0.0f/6.0f;
00286 }
00287 
00289 inline void create_modified_CR_spline_basis(float array[4][4], float slope) {
00290   array[0][0] = -1.0f / slope;
00291   array[0][1] = -1.0f / slope + 2.0f;
00292   array[0][2] =  1.0f / slope - 2.0f;
00293   array[0][3] =  1.0f / slope;
00294   array[1][0] =  2.0f / slope;
00295   array[1][1] =  1.0f / slope - 3.0f;
00296   array[1][2] = -2.0f / slope + 3.0f;
00297   array[1][3] = -1.0f / slope;
00298   array[2][0] = -1.0f / slope;
00299   array[2][1] =  0.0f;
00300   array[2][2] =  1.0f / slope;
00301   array[2][3] =  0.0f;
00302   array[3][0] =  0.0f;
00303   array[3][1] =  1.0f;
00304   array[3][2] =  0.0f;
00305   array[3][3] =  0.0f;
00306 }
00307 
00314 inline void make_spline_Q_matrix(float q[4][3], float basis[4][4], const float *pts) {
00315   int i, j;
00316   for (i = 0; i<4; i++) {
00317     float a, b, c;
00318     a = b = c = 0.0;
00319 
00320     for (j = 0; j<4; j++) {
00321       a += basis[i][j] * pts[j*3    ];
00322       b += basis[i][j] * pts[j*3 + 1];
00323       c += basis[i][j] * pts[j*3 + 2];
00324     }
00325 
00326     q[i][0] = a; 
00327     q[i][1] = b; 
00328     q[i][2] = c; 
00329   }
00330 }
00331 
00338 inline void make_spline_Q_matrix_noncontig(float q[4][3], float basis[4][4], 
00339                                         const float *pts1, const float *pts2,
00340                                         const float *pts3, const float *pts4) {
00341   int i;
00342 
00343   for (i = 0; i<4; i++) {
00344     float a, b, c;
00345     a = b = c = 0.0;
00346 
00347     a += basis[i][0] * pts1[0];
00348     b += basis[i][0] * pts1[1];
00349     c += basis[i][0] * pts1[2];
00350 
00351     a += basis[i][1] * pts2[0];
00352     b += basis[i][1] * pts2[1];
00353     c += basis[i][1] * pts2[2];
00354 
00355     a += basis[i][2] * pts3[0];
00356     b += basis[i][2] * pts3[1];
00357     c += basis[i][2] * pts3[2];
00358 
00359     a += basis[i][3] * pts4[0];
00360     b += basis[i][3] * pts4[1];
00361     c += basis[i][3] * pts4[2];
00362 
00363     q[i][0] = a; 
00364     q[i][1] = b; 
00365     q[i][2] = c; 
00366   }
00367 }
00368 
00369 
00381 inline void make_spline_interpolation(float out[3], float w, float q[4][3]) {
00382   out[0] = w * (w * (w * q[0][0] + q[1][0]) + q[2][0]) + q[3][0];
00383   out[1] = w * (w * (w * q[0][1] + q[1][1]) + q[2][1]) + q[3][1];
00384   out[2] = w * (w * (w * q[0][2] + q[1][2]) + q[2][2]) + q[3][2];
00385 }
00386 
00388 extern int tri_degenerate(const float *, const float *, const float *);
00389 
00391 extern float angle(const float *, const float *);
00392 
00395 extern float dihedral(const float *, const float *, const float *, 
00396                       const float *);
00397 
00399 extern float distance(const float *, const float *);
00400 
00402 inline float distance2(const float *a, const float *b) {
00403   float delta = a[0] - b[0];
00404   float r2 = delta*delta;
00405   delta = a[1] - b[1];
00406   r2 += delta*delta;
00407   delta = a[2] - b[2];
00408   return r2 + delta*delta;
00409 }
00410 
00412 extern float norm(const float *);
00413 
00417 char *vmd_tempfile(const char *);
00418 
00420 int vmd_delete_file(const char *);
00421 
00423 void vmd_sleep(int);   // sleeps for N seconds
00424 void vmd_msleep(int);  // sleeps for N milliseconds
00425 
00428 extern int vmd_system(const char* cmd);
00429 
00430 
00432 extern void vmd_srandom(unsigned int);
00433 extern long vmd_random();
00434 extern float vmd_random_gaussian();
00435 
00436 // select the right VMD_RAND_MAX value depending on implementation and platform
00437 #if defined(__linux) || defined(_MSC_VER)
00438 // Linux uses RAND_MAX for both rand() and random()
00439 // Windows is currently implemented with rand()
00440 #define VMD_RAND_MAX RAND_MAX
00441 #else
00442 // All other platforms I've seen use 2^31-1 for their max random() return val
00443 #if defined(LONG_MAX)
00444 #define VMD_RAND_MAX LONG_MAX
00445 #else
00446 // just in case no LONG_MAX, 
00447 // or in cases where they choose to update it for 64-bits or something
00448 #define VMD_RAND_MAX 2147483647L
00449 #endif
00450 #endif
00451 
00453 long vmd_get_total_physmem_mb(void);
00454 
00456 long vmd_get_avail_physmem_mb(void);
00457 
00459 long vmd_get_avail_physmem_percent(void);
00460 
00461 #endif

Generated on Thu Aug 28 01:26:57 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002