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