1
2#pragma once
3
4#include <cstdlib>
5#include <cmath>
6
7namespace msdfgen {
8
9/// Returns the smaller of the arguments.
10template <typename T>
11inline T min(T a, T b) {
12 return b < a ? b : a;
13}
14
15/// Returns the larger of the arguments.
16template <typename T>
17inline T max(T a, T b) {
18 return a < b ? b : a;
19}
20
21/// Returns the middle out of three values
22template <typename T>
23inline T median(T a, T b, T c) {
24 return max(min(a, b), min(max(a, b), c));
25}
26
27/// Returns the weighted average of a and b.
28template <typename T, typename S>
29inline T mix(T a, T b, S weight) {
30 return T((S(1)-weight)*a+weight*b);
31}
32
33/// Clamps the number to the interval from 0 to 1.
34template <typename T>
35inline T clamp(T n) {
36 return n >= T(0) && n <= T(1) ? n : T(n > T(0));
37}
38
39/// Clamps the number to the interval from 0 to b.
40template <typename T>
41inline T clamp(T n, T b) {
42 return n >= T(0) && n <= b ? n : T(n > T(0))*b;
43}
44
45/// Clamps the number to the interval from a to b.
46template <typename T>
47inline T clamp(T n, T a, T b) {
48 return n >= a && n <= b ? n : n < a ? a : b;
49}
50
51/// Returns 1 for positive values, -1 for negative values, and 0 for zero.
52template <typename T>
53inline int sign(T n) {
54 return (T(0) < n)-(n < T(0));
55}
56
57/// Returns 1 for non-negative values and -1 for negative values.
58template <typename T>
59inline int nonZeroSign(T n) {
60 return 2*(n > T(0))-1;
61}
62
63}
64