| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef MATHUTILS_H |
| 17 |
#define MATHUTILS_H |
| 18 |
|
| 19 |
#include <math.h> |
| 20 |
#include <algorithm> |
| 21 |
|
| 22 |
namespace android { |
| 23 |
namespace uirenderer { |
| 24 |
|
| 25 |
class MathUtils { |
| 26 |
public: |
| 27 |
static constexpr float NON_ZERO_EPSILON = 0.001f; |
| 28 |
static constexpr float ALPHA_EPSILON = 0.001f; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
inline static bool isZero(float value) { |
| 34 |
|
| 35 |
|
| 36 |
return fabsf(value) <= NON_ZERO_EPSILON; |
| 37 |
} |
| 38 |
|
| 39 |
inline static bool isOne(float value) { |
| 40 |
return areEqual(value, 1.0f); |
| 41 |
} |
| 42 |
|
| 43 |
inline static bool isPositive(float value) { return value >= NON_ZERO_EPSILON; } |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
inline static float clampAlpha(float alpha) { |
| 49 |
if (alpha <= ALPHA_EPSILON) { |
| 50 |
return 0; |
| 51 |
} else if (alpha >= (1 - ALPHA_EPSILON)) { |
| 52 |
return 1; |
| 53 |
} else { |
| 54 |
return alpha; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
inline static float clampTessellationScale(float scale) { |
| 62 |
const float MIN_SCALE = 0.0001; |
| 63 |
const float MAX_SCALE = 1e10; |
| 64 |
if (scale < MIN_SCALE) { |
| 65 |
return MIN_SCALE; |
| 66 |
} else if (scale > MAX_SCALE) { |
| 67 |
return MAX_SCALE; |
| 68 |
} |
| 69 |
return scale; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
inline static int divisionsNeededToApproximateArc(float radius, float angleInRads, |
| 77 |
float threshold) { |
| 78 |
const float errConst = (-threshold / radius + 1); |
| 79 |
const float targetCosVal = 2 * errConst * errConst - 1; |
| 80 |
|
| 81 |
|
| 82 |
return (int)(ceilf(angleInRads / acos(targetCosVal) / 2)) * 2; |
| 83 |
} |
| 84 |
|
| 85 |
inline static bool areEqual(float valueA, float valueB) { return isZero(valueA - valueB); } |
| 86 |
|
| 87 |
template <typename T> |
| 88 |
static inline T clamp(T a, T minValue, T maxValue) { |
| 89 |
return std::min(std::max(a, minValue), maxValue); |
| 90 |
} |
| 91 |
|
| 92 |
inline static float lerp(float v1, float v2, float t) { return v1 + ((v2 - v1) * t); } |
| 93 |
}; |
| 94 |
|
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
#endif |