Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef UTIL_INLINEFUNCTIONS_H
00019 #define UTIL_INLINEFUNCTIONS_H
00020
00021 #include "../Util/GlobalDefines.h"
00022
00023 #ifndef _WIN32
00024 #include <unistd.h>
00025 #endif
00026
00027 #include <time.h>
00028 #include <limits.h>
00029
00030 #if defined (_WIN32)
00031 #include <sys/timeb.h>
00032 #include <windows.h>
00033 #include <process.h>
00034 #endif
00035 #ifdef __QNX__
00036 #include <sys/time.h>
00037 #include <time.h>
00038 #include <sys/timeb.h>
00039 #include <unistd.h>
00040 #include <semaphore.h>
00041 #include <signal.h>
00042 #include <i86.h>
00043 #include <process.h>
00044 #define TRACE printf
00045 #define CRITICAL_SECTION int
00046 #endif
00047
00048 #ifdef __LINUX__
00049 #include <sys/time.h>
00050
00051 #include <pthread.h>
00052 #define TRACE printf
00053 #define CRITICAL_SECTION pthread_mutex_t
00054 #endif
00055 #include "../Util/Math.h"
00056
00057
00058 template <class T> inline T sqr(T fValue)
00059 {
00060 return fValue*fValue;
00061 };
00062
00063
00064 template <class T> inline int iRound(T v)
00065 {
00066 return (v>=0) ? (int)(v+.5) : (int)(v-.5);
00067 };
00068
00069
00070 template <class T> inline T util_min(T a, T b)
00071 {
00072 return (a<b) ? a : b;
00073 };
00074
00075
00076 template <class T> inline T util_max(T a, T b)
00077 {
00078 return (a>b) ? a : b;
00079 };
00080
00081 #ifndef NO_ABS_FCT
00082
00083
00084 inline long abs(long iValue)
00085 {
00086 #if defined(NO_CAST_FUNCTION_TEMPLATES)
00087 return long(abs(iValue));
00088 #else
00089 return static_cast<long>(abs(iValue));
00090 #endif
00091 };
00092
00093
00094 inline unsigned long abs(unsigned long uiValue)
00095 {
00096 #if defined(NO_CAST_FUNCTION_TEMPLATES)
00097 return unsigned long(abs(uiValue));
00098 #else
00099 return static_cast<unsigned long>(abs(uiValue));
00100 #endif
00101 };
00102
00103
00104 inline float abs(float fValue)
00105 {
00106 #if defined(NO_CAST_FUNCTION_TEMPLATES)
00107 return float(fabs(fValue));
00108 #else
00109 return static_cast<float>(fabs(fValue));
00110 #endif
00111 };
00112
00113
00114 inline double abs(double fValue)
00115 {
00116 return fabs(fValue);
00117 };
00118
00119 #endif
00120
00121
00122 inline float util_sign(float a, float b)
00123 {
00124 return ((b) >= 0.0) ? fabs(a) : -fabs(a);
00125 };
00126
00127
00128 inline double util_sign(double a, double b)
00129 {
00130 return ((b) >= 0.0) ? fabs(a) : -fabs(a);
00131 };
00132
00133 template <class T> inline void util_shift(T a, T b, T c, T d)
00134 {
00135 (a)=(b);
00136 (b)=(c);
00137 (c)=(d);
00138 }
00139
00140
00141 inline double util_degToRad(double fAngle)
00142 {
00143 return fAngle * M_PI / 180.0;
00144 };
00145
00146
00147 inline double util_radToDeg(double fAngle)
00148 {
00149 return fAngle * 180.0 / M_PI;
00150 };
00151
00152
00153 inline double util_adjustedPhase(double fPhase)
00154 {
00155 return fPhase - (2*M_PI)*floor(fPhase*M_1_2PI);
00156 }
00157
00158
00159 inline double util_phaseDifference(double fPhase1, double fPhase2)
00160 {
00161 return util_adjustedPhase(fPhase1 - fPhase2 + M_PI) - M_PI;
00162 }
00163
00164
00165 inline double util_averagedPhase(double fPhase1, double fPhase2)
00166 {
00167 return util_adjustedPhase(fPhase1 + (util_phaseDifference(fPhase2,fPhase1)*0.5));
00168 }
00169
00170
00171 template <class Type>
00172 inline void util_swap(Type& a, Type& b)
00173 {
00174 Type swappy = a;
00175 a = b; b = swappy;
00176 }
00177
00178 #if defined _WIN32
00179
00180 #ifndef __HAS_SLEEP__
00181 #define __HAS_SLEEP__
00182
00183
00184 inline void sleep(unsigned int uiSec)
00185 {
00186 #if defined(NO_CAST_FUNCTION_TEMPLATES)
00187 Sleep(DWORD(uiSec*1000));
00188 #else
00189 Sleep(static_cast<DWORD> (uiSec*1000));
00190 #endif
00191 }
00192 #endif
00193 #endif
00194
00195 #if defined (__LINUX__)
00196 inline int EnterCriticalSection(CRITICAL_SECTION *cs)
00197 {
00198 pthread_mutex_lock(cs);
00199 return 0;
00200 }
00201
00202 inline int LeaveCriticalSection(CRITICAL_SECTION *cs)
00203 {
00204 pthread_mutex_unlock(cs);
00205 return 0;
00206 }
00207
00208 inline int InitializeCriticalSection(CRITICAL_SECTION *cs)
00209 {
00210 pthread_mutex_init(cs,NULL);
00211 pthread_mutex_unlock(cs);
00212 return 0;
00213 }
00214
00215 inline int DeleteCriticalSection(CRITICAL_SECTION *cs)
00216 {
00217
00218 return 0;
00219 }
00220
00221 inline int Sleep(long iMilliSec)
00222 {
00223 timespec tm, tm2;
00224
00225 tm.tv_sec=iMilliSec/1000;
00226 tm.tv_nsec=(iMilliSec%1000)*1000000;
00227
00228 nanosleep(&tm,&tm2);
00229 return 0;
00230 }
00231 #endif
00232
00233 #if defined (__QNX__)
00234 inline int EnterCriticalSection(CRITICAL_SECTION *cs)
00235 {
00236 sem_wait( (sem_t *)cs );
00237 return 0;
00238 }
00239 inline int LeaveCriticalSection(CRITICAL_SECTION *cs)
00240 {
00241 sem_post( (sem_t *)cs );
00242 return 0;
00243 }
00244 inline int InitializeCriticalSection(CRITICAL_SECTION *cs)
00245 {
00246 sem_init( (sem_t*)cs, 1, 1 );
00247 return 0;
00248 }
00249
00250 inline int DeleteCriticalSection(CRITICAL_SECTION *cs)
00251 {
00252
00253 sem_destroy( (sem_t*)cs );
00254 return 0;
00255 }
00256
00257 inline int Sleep(long iMilliSec)
00258 {
00259 delay(iMilliSec);
00260 return 0;
00261 }
00262 #endif
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 inline unsigned int util_setAlarm(unsigned int uiSec)
00275 {
00276 #ifdef _WIN32
00277
00278 return 0;
00279 #else
00280 return alarm(uiSec);
00281 #endif
00282 };
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 inline unsigned int util_deactivateAlarm()
00294 {
00295 #ifdef _WIN32
00296
00297 return 0;
00298 #else
00299 return alarm(0);
00300
00301 #endif
00302 };
00303
00304 #endif // UTIL_INLINEFUNCTIONS_H