OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
fastMath.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_UTILITIES_MATH_HPP
2 #define OPENPOSE_UTILITIES_MATH_HPP
3 
4 namespace op
5 {
6  // Use op::round/max/min for basic types (int, char, long, float, double, etc). Never with classes!
7  // `std::` alternatives uses 'const T&' instead of 'const T' as argument.
8  // E.g., std::round is really slow (~300 ms vs ~10 ms when I individually apply it to each element of a whole
9  // image array
10 
11  // VERY IMPORTANT: These fast functions does NOT work for negative integer numbers.
12  // E.g., positiveIntRound(-180.f) = -179.
13 
14  // Round functions
15  // Signed
16  template<typename T>
17  inline char positiveCharRound(const T a)
18  {
19  return char(a+0.5f);
20  }
21 
22  template<typename T>
23  inline signed char positiveSCharRound(const T a)
24  {
25  return (signed char)(a+0.5f);
26  }
27 
28  template<typename T>
29  inline int positiveIntRound(const T a)
30  {
31  return int(a+0.5f);
32  }
33 
34  template<typename T>
35  inline long positiveLongRound(const T a)
36  {
37  return long(a+0.5f);
38  }
39 
40  template<typename T>
41  inline long long positiveLongLongRound(const T a)
42  {
43  return (long long)(a+0.5f);
44  }
45 
46  // Unsigned
47  template<typename T>
48  inline unsigned char uCharRound(const T a)
49  {
50  return (unsigned char)(a+0.5f);
51  }
52 
53  template<typename T>
54  inline unsigned int uIntRound(const T a)
55  {
56  return (unsigned int)(a+0.5f);
57  }
58 
59  template<typename T>
60  inline unsigned long ulongRound(const T a)
61  {
62  return (unsigned long)(a+0.5f);
63  }
64 
65  template<typename T>
66  inline unsigned long long uLongLongRound(const T a)
67  {
68  return (unsigned long long)(a+0.5f);
69  }
70 
71  // Max/min functions
72  template<typename T>
73  inline T fastMax(const T a, const T b)
74  {
75  return (a > b ? a : b);
76  }
77 
78  template<typename T>
79  inline T fastMin(const T a, const T b)
80  {
81  return (a < b ? a : b);
82  }
83 
84  template<class T>
85  inline T fastTruncate(T value, T min = 0, T max = 1)
86  {
87  return fastMin(max, fastMax(min, value));
88  }
89 }
90 
91 #endif // OPENPOSE_UTILITIES_MATH_HPP
long long positiveLongLongRound(const T a)
Definition: fastMath.hpp:41
T fastTruncate(T value, T min=0, T max=1)
Definition: fastMath.hpp:85
long positiveLongRound(const T a)
Definition: fastMath.hpp:35
unsigned char uCharRound(const T a)
Definition: fastMath.hpp:48
int positiveIntRound(const T a)
Definition: fastMath.hpp:29
T fastMin(const T a, const T b)
Definition: fastMath.hpp:79
unsigned long long uLongLongRound(const T a)
Definition: fastMath.hpp:66
unsigned int uIntRound(const T a)
Definition: fastMath.hpp:54
T fastMax(const T a, const T b)
Definition: fastMath.hpp:73
unsigned long ulongRound(const T a)
Definition: fastMath.hpp:60
char positiveCharRound(const T a)
Definition: fastMath.hpp:17
signed char positiveSCharRound(const T a)
Definition: fastMath.hpp:23