blob: d89859b519927a049e964fe3a80fe547a744f28d [file] [log] [blame]
Chris Craike0bb87d2014-04-22 17:55:41 -07001/*
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
John Reck1aa5d2d2014-07-24 13:38:28 -070022#define NON_ZERO_EPSILON (0.001f)
John Reck3b52c032014-08-06 10:19:32 -070023#define ALPHA_EPSILON (0.001f)
John Reck1aa5d2d2014-07-24 13:38:28 -070024
Chris Craike0bb87d2014-04-22 17:55:41 -070025class MathUtils {
Chris Craike0bb87d2014-04-22 17:55:41 -070026public:
27 /**
28 * Check for floats that are close enough to zero.
29 */
30 inline static bool isZero(float value) {
John Reck1aa5d2d2014-07-24 13:38:28 -070031 return (value >= -NON_ZERO_EPSILON) && (value <= NON_ZERO_EPSILON);
Chris Craike0bb87d2014-04-22 17:55:41 -070032 }
Chris Craikcc39e162014-04-25 18:34:11 -070033
34 inline static bool isPositive(float value) {
John Reck1aa5d2d2014-07-24 13:38:28 -070035 return value >= NON_ZERO_EPSILON;
Chris Craikcc39e162014-04-25 18:34:11 -070036 }
John Reck315c3292014-05-09 19:21:04 -070037
Chris Craik74cf7e62014-08-07 14:34:46 -070038 /**
39 * Clamps alpha value, and snaps when very near 0 or 1
40 */
John Reck3b52c032014-08-06 10:19:32 -070041 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 Craik74cf7e62014-08-07 14:34:46 -070051 /*
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 Craikdeeda3d2014-05-05 19:09:33 -070065 inline static bool areEqual(float valueA, float valueB) {
66 return isZero(valueA - valueB);
67 }
68
ztenghuic50a03d2014-08-21 13:47:54 -070069 template<typename T>
70 static inline T max(T a, T b) {
Chris Craik05f3d6e2014-06-02 16:27:04 -070071 return a > b ? a : b;
72 }
73
ztenghuic50a03d2014-08-21 13:47:54 -070074 template<typename T>
75 static inline T min(T a, T b) {
John Reck315c3292014-05-09 19:21:04 -070076 return a < b ? a : b;
77 }
78
ztenghui3bd3fa12014-08-25 14:42:27 -070079 template<typename T>
80 static inline T clamp(T a, T minValue, T maxValue) {
81 return min(max(a, minValue), maxValue);
82 }
83
John Reck315c3292014-05-09 19:21:04 -070084 inline static float lerp(float v1, float v2, float t) {
85 return v1 + ((v2 - v1) * t);
86 }
Chris Craike0bb87d2014-04-22 17:55:41 -070087}; // class MathUtils
88
89} /* namespace uirenderer */
90} /* namespace android */
91
Chris Craike4aa95e2014-05-08 13:57:05 -070092#endif /* MATHUTILS_H */