InlineFunctions.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
00003  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *   http://www.apache.org/licenses/LICENSE-2.0
00010 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
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 //#include <linux/delay.h>
00051 #include <pthread.h>
00052 #define TRACE printf
00053 #define CRITICAL_SECTION pthread_mutex_t
00054 #endif
00055 #include "../Util/Math.h"
00056 
00057 //  returns the squared fValue
00058 template <class T> inline T sqr(T fValue)     
00059 { 
00060         return fValue*fValue; 
00061 };
00062 
00063 // returns the rounded integer fValue
00064 template <class T> inline int iRound(T v)      
00065 {
00066         return (v>=0) ? (int)(v+.5) : (int)(v-.5);
00067 };
00068 
00069 // returns the minimum of fValue a and fValue b 
00070 template <class T> inline T util_min(T a, T b)    
00071 { 
00072         return (a<b) ? a : b; 
00073 };
00074 
00075 // returns the maximum of fValue a and fValue b 
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 // returns the absolute fValue
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 // returns the absolute uiValue
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 // returns the absolute fValue 
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 // returns the absolute fValue
00114 inline double abs(double fValue) 
00115 { 
00116         return fabs(fValue);
00117 };
00118 
00119 #endif
00120 
00121 // returns fValue a with the sign of fValue b 
00122 inline float util_sign(float a, float b) 
00123 { 
00124         return ((b) >= 0.0) ? fabs(a) : -fabs(a); 
00125 };
00126 
00127 // returns fValue a with the sign of fValue b 
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 // converts degrees to radians 
00141 inline double util_degToRad(double fAngle)
00142 { 
00143         return fAngle * M_PI / 180.0; 
00144 };
00145 
00146 // converts radians to degrees 
00147 inline double util_radToDeg(double fAngle)
00148 { 
00149         return fAngle * 180.0 / M_PI; 
00150 };
00151 
00152 // fits fPhase into the interval [0,2 \pi[
00153 inline double util_adjustedPhase(double fPhase) 
00154 {
00155         return fPhase - (2*M_PI)*floor(fPhase*M_1_2PI);
00156 }
00157 
00158 // computes fPhase1 - fPhase2 as a fValue of [-pi,pi[
00159 inline double util_phaseDifference(double fPhase1, double fPhase2) 
00160 {
00161         return util_adjustedPhase(fPhase1 - fPhase2 + M_PI) - M_PI;
00162 }
00163 
00164 // computes the average of fPhase1 and fPhase2 as a fValue of [0, 2pi[
00165 inline double util_averagedPhase(double fPhase1, double fPhase2) 
00166 {
00167         return util_adjustedPhase(fPhase1 + (util_phaseDifference(fPhase2,fPhase1)*0.5));
00168 }
00169 
00170 // exhanges the contents of two variables
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 // encapsulates the Win32 version of sleep called Sleep
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 //      pthread_mutex_exit(cs);
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 //      sem_exit((sem_t*) cs);
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 // sets the alarm clock to the specified number of uiSec. 
00267 /*
00268         sets the alarm clock to the specified number of uiSec.
00269           NOTE for UNIX-systems: see the manual pages for alarm(2)
00270           NOTE for WIN32-systems: does nothing! (just returns 0).
00271         uiSec: number of uiSec
00272         the amount of time  previously remaining in the alarm clock.
00273 */
00274 inline unsigned int util_setAlarm(unsigned int uiSec)
00275 {
00276 #ifdef _WIN32
00277 // there does not exist any alarm function for WIN32!
00278         return 0;
00279 #else
00280         return alarm(uiSec);
00281 #endif
00282 };
00283 
00284 // -------------------------------------------------------------------------- ;
00285 
00286 // cancels any previously made alarm request.
00287 /*
00288         cancels any previously made alarm request.
00289           NOTE for UNIX-systems: see the manual pages for alarm(2)
00290           NOTE for WIN32-systems: does nothing! (just returns 0).
00291         the amount of time  previously remaining in the alarm clock.
00292 */
00293 inline unsigned int util_deactivateAlarm()
00294 {
00295 #ifdef _WIN32
00296 // there does not exist any alarm function for WIN32!
00297         return 0;
00298 #else
00299         return alarm(0);   // if number of uiSec is equal 0, any previously
00300                                            // made alarm request is canceled
00301 #endif
00302 };
00303 
00304 #endif // UTIL_INLINEFUNCTIONS_H


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Sat Jun 8 2019 20:25:13