forked from Ares-Developers/YRpp
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathYRMath.h
86 lines (71 loc) · 1.94 KB
/
YRMath.h
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
#pragma once
#include <YRPPCore.h>
#include <type_traits>
#define MATH_FUNC(name, address)\
inline __declspec(naked) double __cdecl name(double value)\
{\
JMP(address);\
}
namespace Math
{
constexpr auto const Pi = 3.1415926535897932384626433832795;
constexpr auto const TwoPi = 6.283185307179586476925286766559;
constexpr auto const HalfPi = 1.5707963267948966192313216916398;
constexpr auto const Sqrt2 = 1.4142135623730950488016887242097;
// Game degrees to radians coefficient, called 'binary angle magic' by some.
constexpr auto const GameDegreesToRadiansCoefficient = -(360.0 / (65535 - 1)) * Pi / 180.0;
constexpr auto const GameDegrees90 = 0X3FFF;
MATH_FUNC(sqrt, 0x4CAC40);
MATH_FUNC(sin, 0x4CACB0);
MATH_FUNC(cos, 0x4CAD00);
MATH_FUNC(tan, 0x4CAD50);
MATH_FUNC(asin, 0x4CAD80);
MATH_FUNC(acos, 0x4CADB0);
MATH_FUNC(atan, 0x4CADE0);
inline __declspec(naked) double __cdecl atan2(double a, double b)
{
JMP(0x4CAE30);
}
inline constexpr double rad2deg(double rad)
{
return rad * 180.0 / Pi;
}
inline constexpr double deg2rad(double deg)
{
return deg * Pi / 180.0;
}
template <typename T>
inline constexpr int sgn(T val) {
// http://stackoverflow.com/a/4609795
return (T(0) < val) - (val < T(0));
}
template <typename T>
using value_return_t = std::remove_cv_t<std::remove_reference_t<T>>;
template <typename T, typename T2>
inline auto min(T&& value, T2&& value2)
{
if(value2 < value) {
return static_cast<value_return_t<T>>(value2);
}
return value;
}
template <typename T, typename T2>
inline auto max(T&& value, T2&& value2)
{
if(value < value2) {
return static_cast<value_return_t<T>>(value2);
}
return value;
}
template <typename T, typename TMin, typename TMax>
inline auto clamp(T&& value, TMin&& min, TMax&& max)
{
if(value < min) {
return static_cast<value_return_t<T>>(min);
}
if(max < value) {
return static_cast<value_return_t<T>>(max);
}
return value;
}
};