OpenPose  1.0.0rc2
OpenPose: A Real-Time Multi-Person Key-Point Detection And Multi-Threading C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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! std:: alternatives uses 'const T&' instead of 'const T' as argument.
7  // E.g. std::round is really slow (~300 ms vs ~10 ms when I individually apply it to each element of a whole image array (e.g. in floatPtrToUCharCvMat)
8 
9  // Round functions
10  // Signed
11  template<typename T>
12  inline char charRound(const T a)
13  {
14  return char(a+0.5f);
15  }
16 
17  template<typename T>
18  inline signed char sCharRound(const T a)
19  {
20  return (signed char)(a+0.5f);
21  }
22 
23  template<typename T>
24  inline int intRound(const T a)
25  {
26  return int(a+0.5f);
27  }
28 
29  template<typename T>
30  inline long longRound(const T a)
31  {
32  return long(a+0.5f);
33  }
34 
35  template<typename T>
36  inline long long longLongRound(const T a)
37  {
38  return (long long)(a+0.5f);
39  }
40 
41  // Unsigned
42  template<typename T>
43  inline unsigned char uCharRound(const T a)
44  {
45  return (unsigned char)(a+0.5f);
46  }
47 
48  template<typename T>
49  inline unsigned int uIntRound(const T a)
50  {
51  return (unsigned int)(a+0.5f);
52  }
53 
54  template<typename T>
55  inline unsigned long ulongRound(const T a)
56  {
57  return (unsigned long)(a+0.5f);
58  }
59 
60  template<typename T>
61  inline unsigned long long uLongLongRound(const T a)
62  {
63  return (unsigned long long)(a+0.5f);
64  }
65 
66  // Max/min functions
67  template<typename T>
68  inline T fastMax(const T a, const T b)
69  {
70  return (a > b ? a : b);
71  }
72 
73  template<typename T>
74  inline T fastMin(const T a, const T b)
75  {
76  return (a < b ? a : b);
77  }
78 
79  template<class T>
80  inline T fastTruncate(T value, T min = 0, T max = 1)
81  {
82  return fastMin(max, fastMax(min, value));
83  }
84 }
85 
86 #endif // OPENPOSE_UTILITIES_MATH_HPP
unsigned int uIntRound(const T a)
Definition: fastMath.hpp:49
T fastMax(const T a, const T b)
Definition: fastMath.hpp:68
T fastTruncate(T value, T min=0, T max=1)
Definition: fastMath.hpp:80
char charRound(const T a)
Definition: fastMath.hpp:12
long longRound(const T a)
Definition: fastMath.hpp:30
unsigned long ulongRound(const T a)
Definition: fastMath.hpp:55
signed char sCharRound(const T a)
Definition: fastMath.hpp:18
unsigned char uCharRound(const T a)
Definition: fastMath.hpp:43
T fastMin(const T a, const T b)
Definition: fastMath.hpp:74
int intRound(const T a)
Definition: fastMath.hpp:24
unsigned long long uLongLongRound(const T a)
Definition: fastMath.hpp:61
long long longLongRound(const T a)
Definition: fastMath.hpp:36