aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/math/math.h
blob: 79ed4c873102ad963c211d11bd18cdac28ed86fa (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef __JE_UTILS_MATH_H__
#define __JE_UTILS_MATH_H__

#include "constant.h"
#include "matrix.h"
#include "quad.h"

namespace JinEngine
{
    namespace Math
    {

    #ifdef min
        #undef min 
    #endif // min
    #ifdef max
        #undef max
    #endif // max

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

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

        template<typename T>
        inline T clamp(T a, T mi, T ma)
        {
            return min<T>(max<T>(a, mi), ma);
        }

        template<typename T>
        inline bool within(T a, T mi, T ma)
        {
            return a >= mi && a <= ma;
        }

        template<typename T>
        inline bool without(T a, T mi, T ma)
        {
            return a < mi || a > ma;
        }

        template<typename T>
        inline T lowerBound(T a, T lower)
        {
            return a < lower ? lower : a;
        }

        template<typename T>
        inline T upperBound(T a, T upper)
        {
            return a > upper ? upper : a;
        }
    
        inline float lerp(float a, float b, float f)
        {
            f = clamp<float>(f, 0, 1);
            return a + f * (b - a);
        }

        template<typename T> 
        inline T abs(T a)
        {
            return a > 0 ? a : -a;
        }

        template<typename T> 
        inline T reverse(T a)
        {
            return -a;
        }

        template<typename T>
        inline T lerp(T a, T b, float t)
        {
            return a + t * (b - a);
        }

        template<typename T>
        inline T slerp(T start, T end, float percent)
        {
            // Dot product - the cosine of the angle between 2 vectors.
            float dot = start * end;
            // Clamp it to be in the range of Acos()
            // This may be unnecessary, but floating point
            // precision can be a fickle mistress.
            dot = clamp<float>(dot, -1.0f, 1.0f);
            // Acos(dot) returns the angle between start and end,
            // And multiplying that by percent returns the angle between
            // start and the final result.
            float theta = Mathf.Acos(dot)*percent;
            Vector3 RelativeVec = end - start * dot;
            RelativeVec.Normalize();
            // Orthonormal basis
            // The final result.
            return ((start*Mathf.Cos(theta)) + (RelativeVec*Mathf.Sin(theta)));
        }

        template<typename T>
        inline T nlerp(T a, T b, float t)
        {

        }

    } // namespace Math
} // namespace JinEngine

#endif // __JE_UTILS_MATH_H__