blob: cc8d83f10d4386b7e71b0b9a1a907ea23775f58d [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
Chris Craikff29b5a2015-05-27 18:17:03 -070019#include <math.h>
John Reck1bcacfd2017-11-03 10:12:19 -070020#include <algorithm>
Chris Craikff29b5a2015-05-27 18:17:03 -070021
Chris Craike0bb87d2014-04-22 17:55:41 -070022namespace android {
23namespace uirenderer {
24
John Reck1aa5d2d2014-07-24 13:38:28 -070025#define NON_ZERO_EPSILON (0.001f)
John Reck3b52c032014-08-06 10:19:32 -070026#define ALPHA_EPSILON (0.001f)
John Reck1aa5d2d2014-07-24 13:38:28 -070027
Chris Craike0bb87d2014-04-22 17:55:41 -070028class MathUtils {
Chris Craike0bb87d2014-04-22 17:55:41 -070029public:
30 /**
31 * Check for floats that are close enough to zero.
32 */
33 inline static bool isZero(float value) {
John Reck1aa5d2d2014-07-24 13:38:28 -070034 return (value >= -NON_ZERO_EPSILON) && (value <= NON_ZERO_EPSILON);
Chris Craike0bb87d2014-04-22 17:55:41 -070035 }
Chris Craikcc39e162014-04-25 18:34:11 -070036
John Reck0aff62d2018-11-26 16:41:34 -080037 inline static bool isOne(float value) {
38 return areEqual(value, 1.0f);
39 }
40
John Reck1bcacfd2017-11-03 10:12:19 -070041 inline static bool isPositive(float value) { return value >= NON_ZERO_EPSILON; }
John Reck315c3292014-05-09 19:21:04 -070042
Chris Craik74cf7e62014-08-07 14:34:46 -070043 /**
44 * Clamps alpha value, and snaps when very near 0 or 1
45 */
John Reck3b52c032014-08-06 10:19:32 -070046 inline static float clampAlpha(float alpha) {
47 if (alpha <= ALPHA_EPSILON) {
48 return 0;
49 } else if (alpha >= (1 - ALPHA_EPSILON)) {
50 return 1;
51 } else {
52 return alpha;
53 }
54 }
55
Chris Craik74cf7e62014-08-07 14:34:46 -070056 /*
57 * Clamps positive tessellation scale values
58 */
59 inline static float clampTessellationScale(float scale) {
60 const float MIN_SCALE = 0.0001;
61 const float MAX_SCALE = 1e10;
62 if (scale < MIN_SCALE) {
63 return MIN_SCALE;
64 } else if (scale > MAX_SCALE) {
65 return MAX_SCALE;
66 }
67 return scale;
68 }
69
Chris Craikff29b5a2015-05-27 18:17:03 -070070 /**
71 * Returns the number of points (beyond two, the start and end) needed to form a polygonal
72 * approximation of an arc, with a given threshold value.
73 */
John Reck1bcacfd2017-11-03 10:12:19 -070074 inline static int divisionsNeededToApproximateArc(float radius, float angleInRads,
75 float threshold) {
Chris Craikff29b5a2015-05-27 18:17:03 -070076 const float errConst = (-threshold / radius + 1);
77 const float targetCosVal = 2 * errConst * errConst - 1;
78
79 // needed divisions are rounded up from approximation
John Reck1bcacfd2017-11-03 10:12:19 -070080 return (int)(ceilf(angleInRads / acos(targetCosVal) / 2)) * 2;
Chris Craikff29b5a2015-05-27 18:17:03 -070081 }
82
John Reck1bcacfd2017-11-03 10:12:19 -070083 inline static bool areEqual(float valueA, float valueB) { return isZero(valueA - valueB); }
Chris Craikdeeda3d2014-05-05 19:09:33 -070084
John Reck1bcacfd2017-11-03 10:12:19 -070085 template <typename T>
ztenghui3bd3fa12014-08-25 14:42:27 -070086 static inline T clamp(T a, T minValue, T maxValue) {
Chris Craik9db58c02015-08-19 15:19:18 -070087 return std::min(std::max(a, minValue), maxValue);
ztenghui3bd3fa12014-08-25 14:42:27 -070088 }
89
John Reck1bcacfd2017-11-03 10:12:19 -070090 inline static float lerp(float v1, float v2, float t) { return v1 + ((v2 - v1) * t); }
91}; // class MathUtils
Chris Craike0bb87d2014-04-22 17:55:41 -070092
93} /* namespace uirenderer */
94} /* namespace android */
95
Chris Craike4aa95e2014-05-08 13:57:05 -070096#endif /* MATHUTILS_H */