forked from z64tools/ext_lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ext_math.h
90 lines (67 loc) · 1.84 KB
/
ext_math.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
87
88
89
90
#ifndef EXT_MATH_H
#define EXT_MATH_H
#include "ext_type.h"
#include "ext_macros.h"
#include <math.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
static inline f32 lerpf(f32 x, f32 min, f32 max) {
return min + (max - min) * x;
}
static inline f32 normf(f32 v, f32 min, f32 max) {
return (v - min) / (max - min);
}
static inline f64 lerpd(f64 x, f64 min, f64 max) {
return min + (max - min) * x;
}
static inline f64 normd(f64 v, f64 min, f64 max) {
return (v - min) / (max - min);
}
static inline int wrap(int x, int min, int max) {
int range = max - min;
if (x < min)
x += range * ((min - x) / range + 1);
return min + (x - min) % range;
}
static inline f32 wrapf(f32 x, f32 min, f32 max) {
f64 range = max - min;
if (x < min)
x += range * roundf((min - x) / range + 1);
return min + fmodf((x - min), range);
}
static inline s32 pingpongi(s32 v, s32 min, s32 max) {
min = wrap(v, min, max * 2);
if (min < max)
return min;
else
return 2 * max - min;
}
static inline f32 pingpongf(f32 v, f32 min, f32 max) {
min = wrapf(v, min, max * 2);
if (min < max)
return min;
else
return 2 * max - min;
}
static inline f32 Closest(f32 sample, f32 x, f32 y) {
if ((fminf(sample, x) - fmaxf(sample, x) > fminf(sample, y) - fmaxf(sample, y)))
return x;
return y;
}
static inline f32 remapf(f32 v, f32 iMin, f32 iMax, f32 oMin, f32 oMax) {
return lerpf(normf(v, iMin, iMax), oMin, oMax);
}
static inline f64 remapd(f64 v, f64 iMin, f64 iMax, f64 oMin, f64 oMax) {
return lerpd(normd(v, iMin, iMax), oMin, oMax);
}
static inline f32 AccuracyF(f32 v, f32 mod) {
return rint(rint(v * mod) / mod);
}
static inline f32 roundstepf(f32 v, f32 step) {
return rintf(v * (1.0f / step)) * step;
}
static inline f32 invertf(f32 v) {
return remapf(v, 0.0f, 1.0f, 1.0f, 0.0f);
}
#pragma GCC diagnostic pop
#endif