Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #ifndef MATHUTILS_H |
| 17 | #define MATHUTILS_H |
| 18 | |
Chris Craik | ff29b5a | 2015-05-27 18:17:03 -0700 | [diff] [blame] | 19 | #include <math.h> |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Chris Craik | ff29b5a | 2015-05-27 18:17:03 -0700 | [diff] [blame] | 21 | |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | class MathUtils { |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 26 | public: |
John Reck | 9a7c192 | 2021-02-08 19:32:21 -0500 | [diff] [blame] | 27 | static constexpr float NON_ZERO_EPSILON = 0.001f; |
| 28 | static constexpr float ALPHA_EPSILON = 0.001f; |
| 29 | |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 30 | /** |
| 31 | * Check for floats that are close enough to zero. |
| 32 | */ |
| 33 | inline static bool isZero(float value) { |
Edgar Arriaga | 1af13c8 | 2020-06-24 13:06:55 -0700 | [diff] [blame] | 34 | // Using fabsf is more performant as ARM computes |
| 35 | // fabsf in a single instruction. |
| 36 | return fabsf(value) <= NON_ZERO_EPSILON; |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 37 | } |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 38 | |
John Reck | 0aff62d | 2018-11-26 16:41:34 -0800 | [diff] [blame] | 39 | inline static bool isOne(float value) { |
| 40 | return areEqual(value, 1.0f); |
| 41 | } |
| 42 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 43 | inline static bool isPositive(float value) { return value >= NON_ZERO_EPSILON; } |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 44 | |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Clamps alpha value, and snaps when very near 0 or 1 |
| 47 | */ |
John Reck | 3b52c03 | 2014-08-06 10:19:32 -0700 | [diff] [blame] | 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 | |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Clamps positive tessellation scale values |
| 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 | |
Chris Craik | ff29b5a | 2015-05-27 18:17:03 -0700 | [diff] [blame] | 72 | /** |
| 73 | * Returns the number of points (beyond two, the start and end) needed to form a polygonal |
| 74 | * approximation of an arc, with a given threshold value. |
| 75 | */ |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 76 | inline static int divisionsNeededToApproximateArc(float radius, float angleInRads, |
| 77 | float threshold) { |
Chris Craik | ff29b5a | 2015-05-27 18:17:03 -0700 | [diff] [blame] | 78 | const float errConst = (-threshold / radius + 1); |
| 79 | const float targetCosVal = 2 * errConst * errConst - 1; |
| 80 | |
| 81 | // needed divisions are rounded up from approximation |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 82 | return (int)(ceilf(angleInRads / acos(targetCosVal) / 2)) * 2; |
Chris Craik | ff29b5a | 2015-05-27 18:17:03 -0700 | [diff] [blame] | 83 | } |
| 84 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 85 | inline static bool areEqual(float valueA, float valueB) { return isZero(valueA - valueB); } |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 86 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | template <typename T> |
ztenghui | 3bd3fa1 | 2014-08-25 14:42:27 -0700 | [diff] [blame] | 88 | static inline T clamp(T a, T minValue, T maxValue) { |
Chris Craik | 9db58c0 | 2015-08-19 15:19:18 -0700 | [diff] [blame] | 89 | return std::min(std::max(a, minValue), maxValue); |
ztenghui | 3bd3fa1 | 2014-08-25 14:42:27 -0700 | [diff] [blame] | 90 | } |
| 91 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 92 | inline static float lerp(float v1, float v2, float t) { return v1 + ((v2 - v1) * t); } |
| 93 | }; // class MathUtils |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 94 | |
| 95 | } /* namespace uirenderer */ |
| 96 | } /* namespace android */ |
| 97 | |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 98 | #endif /* MATHUTILS_H */ |