-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
58 lines (49 loc) · 1.29 KB
/
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
57
58
//
// Created by Alex Chi on 2018/11/17.
//
#ifndef CAHRR_UTILS_H
#define CAHRR_UTILS_H
#include <cmath>
template<typename T>
inline T clamp(T in, T min, T max) {
return (in < min ? min : (in > max ? max : in));
}
template<typename T>
inline T cycle_err(T err, T range) {
if (abs(err) > range / 2) {
if (err > 0) {
return err - range;
} else {
return err + range;
}
}
return err;
}
template<typename T>
inline T cycle_err(T target, T current, T min, T max) {
T range = max - min;
T err = (target - current) % range;
return cycle_err(err, range);
}
inline double cycle_err_double(double err, double range) {
if (fabs(err) > range / 2) {
if (err > 0) {
return err - range;
} else {
return err + range;
}
}
return err;
}
inline double cycle_err_double(double target, double current, double min, double max) {
double range = max - min;
double err = fmod((target - current), range);
return cycle_err_double(err, range);
}
template <typename T>
inline T map_range(T in, T in_min, T in_max, T out_min, T out_max) {
T out_range = out_max - out_min;
T in_range = in_max - in_min;
return (in - in_min) * out_range / in_range + out_min;
}
#endif //CAHRR_UTILS_H