MathUtils.h revision 3b52c03f5035b833d365215420739aa840ac5080
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 19namespace android { 20namespace uirenderer { 21 22#define NON_ZERO_EPSILON (0.001f) 23#define ALPHA_EPSILON (0.001f) 24 25class MathUtils { 26public: 27 /** 28 * Check for floats that are close enough to zero. 29 */ 30 inline static bool isZero(float value) { 31 return (value >= -NON_ZERO_EPSILON) && (value <= NON_ZERO_EPSILON); 32 } 33 34 inline static bool isPositive(float value) { 35 return value >= NON_ZERO_EPSILON; 36 } 37 38 inline static float clampAlpha(float alpha) { 39 if (alpha <= ALPHA_EPSILON) { 40 return 0; 41 } else if (alpha >= (1 - ALPHA_EPSILON)) { 42 return 1; 43 } else { 44 return alpha; 45 } 46 } 47 48 inline static bool areEqual(float valueA, float valueB) { 49 return isZero(valueA - valueB); 50 } 51 52 inline static int max(int a, int b) { 53 return a > b ? a : b; 54 } 55 56 inline static int min(int a, int b) { 57 return a < b ? a : b; 58 } 59 60 inline static float lerp(float v1, float v2, float t) { 61 return v1 + ((v2 - v1) * t); 62 } 63}; // class MathUtils 64 65} /* namespace uirenderer */ 66} /* namespace android */ 67 68#endif /* MATHUTILS_H */ 69