forked from vimaec/vim-format
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_ops.h
More file actions
262 lines (195 loc) · 9.49 KB
/
math_ops.h
File metadata and controls
262 lines (195 loc) · 9.49 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#pragma once
#include <type_traits>
#include "constants.h"
namespace vim::math3d::mathOps {
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
abs(T x) { return std::abs(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
acos(T x) { return std::acos(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
asin(T x) { return std::asin(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
atan(T x) { return std::atan(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
cos(T x) { return std::cos(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
cosh(T x) { return std::cosh(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
exp(T x) { return std::exp(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
log(T x) { return std::log(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
log10(T x) { return std::log10(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
sin(T x) { return std::sin(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
sinh(T x) { return std::sinh(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
sqrt(T x) { return std::sqrt(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
tan(T x) { return std::tan(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
tanh(T x) { return std::tanh(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, std::int8_t>::type
sign(T x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
magnitude(T x) { return x; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
magnitudeSquared(T x) { return x * x; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
inverse(T x) { return (T)1 / x; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
truncate(T x) { return std::trunc(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
ceiling(T x) { return std::ceil(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
floor(T x) { return std::floor(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
round(T x) { return std::round(x); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
toRadians(T x) { return x * constants::degreesToRadians; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
toDegrees(T x) { return x * constants::radiansToDegrees; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
distance(T v1, T v2) { return std::abs(v2 - v1); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
isInfinity(T v) { return std::isinf(v); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
isNaN(T v) { return std::isnan(v); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
almostZero(T v, float tolerance = constants::tolerance) { return std::fabs(v) < tolerance; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
almostEquals(T v1, T v2, float tolerance = constants::tolerance) { return almostZero(v2 - v1, tolerance); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
smoothstep(T v) { return v * v * (3 - 2 * v); }
template <typename T>
inline T add(T v1, T v2) { return v1 + v2; }
template <typename T>
inline T subtract(T v1, T v2) { return v1 - v2; }
template <typename T>
inline T multiply(T v1, T v2) { return v1 * v2; }
template <typename T>
inline T divide(T v1, T v2) { return v1 / v2; }
template <typename T>
inline T negate(T v) { return -v; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
within(T v, T min, T max) { return v >= min && v < max; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
sqr(T x) { return x * x; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
cube(T x) { return x * x * x; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
min(T v1, T v2) { return std::min(v1, v2); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
max(T v1, T v2) { return std::max(v1, v2); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
gt(T v1, T v2) { return v1 > v2; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
lt(T v1, T v2) { return v1 < v2; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
gt_eq(T v1, T v2) { return v1 >= v2; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
lt_eq(T v1, T v2) { return v1 <= v2; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
eq(T v1, T v2) { return v1 == v2; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
neq(T v1, T v2) { return v1 != v2; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
and_op(T a, T b) { return a & b; }
inline bool and_op(bool a, bool b) { return a && b; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
or_op(T a, T b) { return a | b; }
inline bool or_op(bool a, bool b) { return a || b; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
nand_op(T a, T b) { return ~(a & b); }
inline bool nand_op(bool a, bool b) { return !(a && b); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
xor_op(T a, T b) { return a ^ b; }
inline bool xor_op(bool a, bool b) { return a != b; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
nor_op(T a, T b) { return ~(a | b); }
inline bool nor_op(bool a, bool b) { return !(a || b); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
not_op(T a) { return ~a; }
inline bool not_op(bool a) { return !a; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
divideRoundUp(T a, T b) { return (a + b - 1) / b; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
isEven(T n) { return n % 2 == 0; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
isOdd(T n) { return n % 2 == 1; }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
isPowerOfTwo(T v) { return v > 0 && !(v & (v - 1)); }
template <typename T>
inline T lerp(T v1, T v2, float t) { return v1 + (v2 - v1) * t; }
template <typename T>
inline T inverseLerp(T v, T a, T b) { return (v - a) / (b - a); }
template <typename T>
inline T lerpPrecise(T v1, T v2, float t) { return ((1 - t) * v1) + (v2 * t); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
clampLower(T v, T min) { return std::max(v, min); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
clampUpper(T v, T max) { return std::min(v, max); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
clamp(T v, T min, T max) { return std::max(std::min(v, max), min); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
average(T v1, T v2) { return lerp(v1, v2, 0.5f); }
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
barycentric(T v1, T v2, T v3, T u, T v) { return v1 + (v2 - v1) * u + (v3 - v1) * v; }
}