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 | |
| 19 | namespace android { |
| 20 | namespace uirenderer { |
| 21 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 22 | #define NON_ZERO_EPSILON (0.001f) |
John Reck | 3b52c03 | 2014-08-06 10:19:32 -0700 | [diff] [blame] | 23 | #define ALPHA_EPSILON (0.001f) |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 24 | |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 25 | class MathUtils { |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 26 | public: |
| 27 | /** |
| 28 | * Check for floats that are close enough to zero. |
| 29 | */ |
| 30 | inline static bool isZero(float value) { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 31 | return (value >= -NON_ZERO_EPSILON) && (value <= NON_ZERO_EPSILON); |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 32 | } |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 33 | |
| 34 | inline static bool isPositive(float value) { |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 35 | return value >= NON_ZERO_EPSILON; |
Chris Craik | cc39e16 | 2014-04-25 18:34:11 -0700 | [diff] [blame] | 36 | } |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 37 | |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Clamps alpha value, and snaps when very near 0 or 1 |
| 40 | */ |
John Reck | 3b52c03 | 2014-08-06 10:19:32 -0700 | [diff] [blame] | 41 | inline static float clampAlpha(float alpha) { |
| 42 | if (alpha <= ALPHA_EPSILON) { |
| 43 | return 0; |
| 44 | } else if (alpha >= (1 - ALPHA_EPSILON)) { |
| 45 | return 1; |
| 46 | } else { |
| 47 | return alpha; |
| 48 | } |
| 49 | } |
| 50 | |
Chris Craik | 74cf7e6 | 2014-08-07 14:34:46 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Clamps positive tessellation scale values |
| 53 | */ |
| 54 | inline static float clampTessellationScale(float scale) { |
| 55 | const float MIN_SCALE = 0.0001; |
| 56 | const float MAX_SCALE = 1e10; |
| 57 | if (scale < MIN_SCALE) { |
| 58 | return MIN_SCALE; |
| 59 | } else if (scale > MAX_SCALE) { |
| 60 | return MAX_SCALE; |
| 61 | } |
| 62 | return scale; |
| 63 | } |
| 64 | |
Chris Craik | deeda3d | 2014-05-05 19:09:33 -0700 | [diff] [blame] | 65 | inline static bool areEqual(float valueA, float valueB) { |
| 66 | return isZero(valueA - valueB); |
| 67 | } |
| 68 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 69 | inline static int max(int a, int b) { |
| 70 | return a > b ? a : b; |
| 71 | } |
| 72 | |
John Reck | 315c329 | 2014-05-09 19:21:04 -0700 | [diff] [blame] | 73 | inline static int min(int a, int b) { |
| 74 | return a < b ? a : b; |
| 75 | } |
| 76 | |
| 77 | inline static float lerp(float v1, float v2, float t) { |
| 78 | return v1 + ((v2 - v1) * t); |
| 79 | } |
Chris Craik | e0bb87d | 2014-04-22 17:55:41 -0700 | [diff] [blame] | 80 | }; // class MathUtils |
| 81 | |
| 82 | } /* namespace uirenderer */ |
| 83 | } /* namespace android */ |
| 84 | |
Chris Craik | e4aa95e | 2014-05-08 13:57:05 -0700 | [diff] [blame] | 85 | #endif /* MATHUTILS_H */ |