-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.h
56 lines (47 loc) · 990 Bytes
/
Utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Utils.h
*
* Created on: 23/nov/2016
* Author: bertini
*/
#ifndef UTILS_H_
#define UTILS_H_
template<typename T>
static inline T _abs(const T &a) {
return a < 0 ? -a : a;
}
static inline bool almostEqualFloat(float A, float B, float eps) {
if (A == 0) {
return _abs(B) < eps;
} else if (B == 0) {
return _abs(A) < eps;
} else {
#if 0
float d = max(_abs(A), _abs(B));
float g = (_abs(A - B) / d);
#else
float g = _abs(A - B);
#endif
if (g <= eps) {
return true;
} else {
return false;
}
}
}
static inline bool almostEqualFloat(float A, float B) {
return almostEqualFloat(A, B, 0.2f);
}
static inline bool almostUnequalFloat(float a, float b) {
return !almostEqualFloat(a, b);
}
static inline float _min(float x, float y) {
return x < y ? x : y;
}
static inline float _max(float x, float y) {
return x > y ? x : y;
}
static inline float clamp(float x, float start, float end) {
return _min(_max(x, start), end);
}
#endif /* UTILS_H_ */