InlineFunctions.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 SCHUNK GmbH & Co. KG
3  * Copyright (c) 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef UTIL_INLINEFUNCTIONS_H
19 #define UTIL_INLINEFUNCTIONS_H
20 
21 #include "../Util/GlobalDefines.h"
22 
23 #ifndef _WIN32
24 #include <unistd.h>
25 #endif
26 
27 #include <time.h>
28 #include <limits.h>
29 
30 #if defined (_WIN32)
31 #include <sys/timeb.h>
32 #include <windows.h>
33 #include <process.h>
34 #endif
35 #ifdef __QNX__
36 #include <sys/time.h>
37 #include <time.h>
38 #include <sys/timeb.h>
39 #include <unistd.h>
40 #include <semaphore.h>
41 #include <signal.h>
42 #include <i86.h>
43 #include <process.h>
44 #define TRACE printf
45 #define CRITICAL_SECTION int
46 #endif
47 
48 #ifdef __LINUX__
49 #include <sys/time.h>
50 //#include <linux/delay.h>
51 #include <pthread.h>
52 #define TRACE printf
53 #define CRITICAL_SECTION pthread_mutex_t
54 #endif
55 #include "../Util/Math.h"
56 
57 // returns the squared fValue
58 template <class T> inline T sqr(T fValue)
59 {
60  return fValue*fValue;
61 };
62 
63 // returns the rounded integer fValue
64 template <class T> inline int iRound(T v)
65 {
66  return (v>=0) ? (int)(v+.5) : (int)(v-.5);
67 };
68 
69 // returns the minimum of fValue a and fValue b
70 template <class T> inline T util_min(T a, T b)
71 {
72  return (a<b) ? a : b;
73 };
74 
75 // returns the maximum of fValue a and fValue b
76 template <class T> inline T util_max(T a, T b)
77 {
78  return (a>b) ? a : b;
79 };
80 
81 #ifndef NO_ABS_FCT
82 
83 // returns the absolute fValue
84 inline long abs(long iValue)
85 {
86 #if defined(NO_CAST_FUNCTION_TEMPLATES)
87  return long(abs(iValue));
88 #else
89  return static_cast<long>(abs(iValue));
90 #endif
91 };
92 
93 // returns the absolute uiValue
94 inline unsigned long abs(unsigned long uiValue)
95 {
96 #if defined(NO_CAST_FUNCTION_TEMPLATES)
97  return unsigned long(abs(uiValue));
98 #else
99  return static_cast<unsigned long>(abs(uiValue));
100 #endif
101 };
102 
103 // returns the absolute fValue
104 inline float abs(float fValue)
105 {
106 #if defined(NO_CAST_FUNCTION_TEMPLATES)
107  return float(fabs(fValue));
108 #else
109  return static_cast<float>(fabs(fValue));
110 #endif
111 };
112 
113 // returns the absolute fValue
114 inline double abs(double fValue)
115 {
116  return fabs(fValue);
117 };
118 
119 #endif
120 
121 // returns fValue a with the sign of fValue b
122 inline float util_sign(float a, float b)
123 {
124  return ((b) >= 0.0) ? fabs(a) : -fabs(a);
125 };
126 
127 // returns fValue a with the sign of fValue b
128 inline double util_sign(double a, double b)
129 {
130  return ((b) >= 0.0) ? fabs(a) : -fabs(a);
131 };
132 
133 template <class T> inline void util_shift(T a, T b, T c, T d)
134 {
135  (a)=(b);
136  (b)=(c);
137  (c)=(d);
138 }
139 
140 // converts degrees to radians
141 inline double util_degToRad(double fAngle)
142 {
143  return fAngle * M_PI / 180.0;
144 };
145 
146 // converts radians to degrees
147 inline double util_radToDeg(double fAngle)
148 {
149  return fAngle * 180.0 / M_PI;
150 };
151 
152 // fits fPhase into the interval [0,2 \pi[
153 inline double util_adjustedPhase(double fPhase)
154 {
155  return fPhase - (2*M_PI)*floor(fPhase*M_1_2PI);
156 }
157 
158 // computes fPhase1 - fPhase2 as a fValue of [-pi,pi[
159 inline double util_phaseDifference(double fPhase1, double fPhase2)
160 {
161  return util_adjustedPhase(fPhase1 - fPhase2 + M_PI) - M_PI;
162 }
163 
164 // computes the average of fPhase1 and fPhase2 as a fValue of [0, 2pi[
165 inline double util_averagedPhase(double fPhase1, double fPhase2)
166 {
167  return util_adjustedPhase(fPhase1 + (util_phaseDifference(fPhase2,fPhase1)*0.5));
168 }
169 
170 // exhanges the contents of two variables
171 template <class Type>
172 inline void util_swap(Type& a, Type& b)
173 {
174  Type swappy = a;
175  a = b; b = swappy;
176 }
177 
178 #if defined _WIN32
179 
180 #ifndef __HAS_SLEEP__
181 #define __HAS_SLEEP__
182 
183 // encapsulates the Win32 version of sleep called Sleep
184 inline void sleep(unsigned int uiSec)
185 {
186 #if defined(NO_CAST_FUNCTION_TEMPLATES)
187  Sleep(DWORD(uiSec*1000));
188 #else
189  Sleep(static_cast<DWORD> (uiSec*1000));
190 #endif
191 }
192 #endif
193 #endif
194 
195 #if defined (__LINUX__)
196 inline int EnterCriticalSection(CRITICAL_SECTION *cs)
197 {
198  pthread_mutex_lock(cs);
199  return 0;
200 }
201 
202 inline int LeaveCriticalSection(CRITICAL_SECTION *cs)
203 {
204  pthread_mutex_unlock(cs);
205  return 0;
206 }
207 
208 inline int InitializeCriticalSection(CRITICAL_SECTION *cs)
209 {
210  pthread_mutex_init(cs,NULL);
211  pthread_mutex_unlock(cs);
212  return 0;
213 }
214 
215 inline int DeleteCriticalSection(CRITICAL_SECTION *cs)
216 {
217 // pthread_mutex_exit(cs);
218  return 0;
219 }
220 
221 inline int Sleep(long iMilliSec)
222 {
223  timespec tm, tm2;
224 
225  tm.tv_sec=iMilliSec/1000;
226  tm.tv_nsec=(iMilliSec%1000)*1000000;
227 
228  nanosleep(&tm,&tm2);
229  return 0;
230 }
231 #endif
232 
233 #if defined (__QNX__)
234 inline int EnterCriticalSection(CRITICAL_SECTION *cs)
235 {
236  sem_wait( (sem_t *)cs );
237  return 0;
238 }
239 inline int LeaveCriticalSection(CRITICAL_SECTION *cs)
240 {
241  sem_post( (sem_t *)cs );
242  return 0;
243 }
244 inline int InitializeCriticalSection(CRITICAL_SECTION *cs)
245 {
246  sem_init( (sem_t*)cs, 1, 1 );
247  return 0;
248 }
249 
250 inline int DeleteCriticalSection(CRITICAL_SECTION *cs)
251 {
252 // sem_exit((sem_t*) cs);
253  sem_destroy( (sem_t*)cs );
254  return 0;
255 }
256 
257 inline int Sleep(long iMilliSec)
258 {
259  delay(iMilliSec);
260  return 0;
261 }
262 #endif
263 
264 // -------------------------------------------------------------------------- ;
265 
266 // sets the alarm clock to the specified number of uiSec.
267 /*
268  sets the alarm clock to the specified number of uiSec.
269  NOTE for UNIX-systems: see the manual pages for alarm(2)
270  NOTE for WIN32-systems: does nothing! (just returns 0).
271  uiSec: number of uiSec
272  the amount of time previously remaining in the alarm clock.
273 */
274 inline unsigned int util_setAlarm(unsigned int uiSec)
275 {
276 #ifdef _WIN32
277 // there does not exist any alarm function for WIN32!
278  return 0;
279 #else
280  return alarm(uiSec);
281 #endif
282 };
283 
284 // -------------------------------------------------------------------------- ;
285 
286 // cancels any previously made alarm request.
287 /*
288  cancels any previously made alarm request.
289  NOTE for UNIX-systems: see the manual pages for alarm(2)
290  NOTE for WIN32-systems: does nothing! (just returns 0).
291  the amount of time previously remaining in the alarm clock.
292 */
293 inline unsigned int util_deactivateAlarm()
294 {
295 #ifdef _WIN32
296 // there does not exist any alarm function for WIN32!
297  return 0;
298 #else
299  return alarm(0); // if number of uiSec is equal 0, any previously
300  // made alarm request is canceled
301 #endif
302 };
303 
304 #endif // UTIL_INLINEFUNCTIONS_H
double util_phaseDifference(double fPhase1, double fPhase2)
unsigned int util_setAlarm(unsigned int uiSec)
int iRound(T v)
void util_shift(T a, T b, T c, T d)
double util_averagedPhase(double fPhase1, double fPhase2)
double util_degToRad(double fAngle)
T util_min(T a, T b)
double util_radToDeg(double fAngle)
float util_sign(float a, float b)
T sqr(T fValue)
T util_max(T a, T b)
#define M_1_2PI
Definition: Math.h:55
unsigned int util_deactivateAlarm()
double util_adjustedPhase(double fPhase)
long abs(long iValue)
void util_swap(Type &a, Type &b)


schunk_libm5api
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:19