File size: 2,126 Bytes
7fc5a59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef OPENPOSE_UTILITIES_MATH_HPP
#define OPENPOSE_UTILITIES_MATH_HPP

namespace op
{
    // 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.
    // 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

    // VERY IMPORTANT: These fast functions does NOT work for negative integer numbers.
    // E.g., positiveIntRound(-180.f) = -179.

    // Round functions
    // Signed
    template<typename T>
    inline char positiveCharRound(const T a)
    {
        return char(a+0.5f);
    }

    template<typename T>
    inline signed char positiveSCharRound(const T a)
    {
        return (signed char)(a+0.5f);
    }

    template<typename T>
    inline int positiveIntRound(const T a)
    {
        return int(a+0.5f);
    }

    template<typename T>
    inline long positiveLongRound(const T a)
    {
        return long(a+0.5f);
    }

    template<typename T>
    inline long long positiveLongLongRound(const T a)
    {
        return (long long)(a+0.5f);
    }

    // Unsigned
    template<typename T>
    inline unsigned char uCharRound(const T a)
    {
        return (unsigned char)(a+0.5f);
    }

    template<typename T>
    inline unsigned int uIntRound(const T a)
    {
        return (unsigned int)(a+0.5f);
    }

    template<typename T>
    inline unsigned long ulongRound(const T a)
    {
        return (unsigned long)(a+0.5f);
    }

    template<typename T>
    inline unsigned long long uLongLongRound(const T a)
    {
        return (unsigned long long)(a+0.5f);
    }

    // Max/min functions
    template<typename T>
    inline T fastMax(const T a, const T b)
    {
        return (a > b ? a : b);
    }

    template<typename T>
    inline T fastMin(const T a, const T b)
    {
        return (a < b ? a : b);
    }

    template<class T>
    inline T fastTruncate(T value, T min = 0, T max = 1)
    {
        return fastMin(max, fastMax(min, value));
    }
}

#endif // OPENPOSE_UTILITIES_MATH_HPP