| 1 | |
| 2 | #pragma once |
| 3 | |
| 4 | #include <cstdlib> |
| 5 | #include <cmath> |
| 6 | |
| 7 | namespace msdfgen { |
| 8 | |
| 9 | /// Returns the smaller of the arguments. |
| 10 | template <typename T> |
| 11 | inline T min(T a, T b) { |
| 12 | return b < a ? b : a; |
| 13 | } |
| 14 | |
| 15 | /// Returns the larger of the arguments. |
| 16 | template <typename T> |
| 17 | inline T max(T a, T b) { |
| 18 | return a < b ? b : a; |
| 19 | } |
| 20 | |
| 21 | /// Returns the middle out of three values |
| 22 | template <typename T> |
| 23 | inline 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. |
| 28 | template <typename T, typename S> |
| 29 | inline 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. |
| 34 | template <typename T> |
| 35 | inline 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. |
| 40 | template <typename T> |
| 41 | inline 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. |
| 46 | template <typename T> |
| 47 | inline 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. |
| 52 | template <typename T> |
| 53 | inline 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. |
| 58 | template <typename T> |
| 59 | inline int nonZeroSign(T n) { |
| 60 | return 2*(n > T(0))-1; |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |