diff options
Diffstat (limited to 'src/libjin/math/math.h')
-rw-r--r-- | src/libjin/math/math.h | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/libjin/math/math.h b/src/libjin/math/math.h new file mode 100644 index 0000000..79ed4c8 --- /dev/null +++ b/src/libjin/math/math.h @@ -0,0 +1,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__
\ No newline at end of file |