• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef API_BASE_MATH_MATHF_H
17 #define API_BASE_MATH_MATHF_H
18 
19 #include <cmath>
20 
21 #include <base/namespace.h>
22 
BASE_BEGIN_NAMESPACE()23 BASE_BEGIN_NAMESPACE()
24 namespace Math {
25 constexpr const float BASE_EPSILON = 1.192092896e-07f; // smallest such that 1.0+FLT_EPSILON != 1.0
26 
27 /** \addtogroup group_math_mathf
28  *  @{
29  */
30 /** Returns Minimum value of 2 given parameter */
31 template<typename T>
32 static constexpr inline const T& min(const T& a, const T& b) noexcept
33 {
34     return a < b ? a : b;
35 }
36 
37 /** Returns Maximum value of 2 given parameter */
38 template<typename T>
39 static constexpr inline const T& max(const T& a, const T& b) noexcept
40 {
41     return a > b ? a : b;
42 }
43 
44 /** Clamps value between minimum and maximum value */
45 static constexpr inline float clamp(float value, float min, float max)
46 {
47     if (value < min) {
48         value = min;
49     } else if (value > max) {
50         value = max;
51     }
52     return value;
53 }
54 
55 /** Clamps value between 0 and 1 and then returns it */
56 static constexpr inline float clamp01(float value)
57 {
58     if (value < 0) {
59         return 0;
60     } else if (value > 1) {
61         return 1;
62     } else {
63         return value;
64     }
65 }
66 
67 /** Returns Absolute value of given float */
68 static constexpr inline float abs(float value)
69 {
70     return value < 0 ? -value : value;
71 }
72 
73 /** Returns Absolute value of given integer */
74 static constexpr inline int abs(int value)
75 {
76     return value < 0 ? -value : value;
77 }
78 
79 /** Floor the given float value */
80 static float floor(float value)
81 {
82     return floorf(value);
83 }
84 
85 /** Ceil the given float value */
86 static float ceil(float value)
87 {
88     return ceilf(value);
89 }
90 
91 /** Returns largest integer smaller to or equal to value */
92 static int floorToInt(float value)
93 {
94     return static_cast<int>(floor(value));
95 }
96 
97 /** Returns smallest integer greater to or equal to value */
98 static int ceilToInt(float value)
99 {
100     return static_cast<int>(ceil(value));
101 }
102 
103 /** Interpolates between floats a and b by t and t is clamped value between 0 and 1 */
104 static constexpr inline float lerp(float a, float b, float t)
105 {
106     return a + (b - a) * clamp01(t);
107 }
108 
109 /** Returns value rounded to the nearest integer */
110 static float round(float value)
111 {
112     return roundf(value);
113 }
114 
115 /** Returns square root of value */
116 static float sqrt(float value)
117 {
118     return sqrtf(value);
119 }
120 
121 /** Returns the sine of angle value in radians */
122 static float sin(float value)
123 {
124     return sinf(value);
125 }
126 
127 /** Returns the cosine of angle value in radians */
128 static float cos(float value)
129 {
130     return cosf(value);
131 }
132 
133 /** Returns the tangent of angle value in radians */
134 static float tan(float value)
135 {
136     return tanf(value);
137 }
138 
139 /** Returns f raised to power p */
140 static float pow(float f, float p)
141 {
142     return powf(f, p);
143 }
144 
145 /** atan wrapper for float */
146 static float atan(float f)
147 {
148     return atanf(f);
149 }
150 
151 /** atan2 wrapper for float */
152 static float atan2(float a, float b)
153 {
154     return atan2f(a, b);
155 }
156 
157 /** asin wrapper for float */
158 static float asin(float value)
159 {
160     return asinf(value);
161 }
162 
163 /** acos wrapper for float */
164 static float acos(float value)
165 {
166     return acosf(value);
167 }
168 
169 /** Epsilon (1.192092896e-07F) */
170 static constexpr float EPSILON = BASE_EPSILON;
171 
172 /** Positive infinity */
173 static float infinity = INFINITY;
174 
175 /** 3.14... */
176 static constexpr float PI = 3.141592653f;
177 
178 /** Degrees-to-radians conversion constant */
179 static constexpr float DEG2RAD = PI * 2.0f / 360.0f;
180 
181 /** Radians-to-degrees conversion constant */
182 static constexpr float RAD2DEG = 1.0f / DEG2RAD;
183 
184 /** Returns the sign of value */
185 static constexpr inline float sign(float value)
186 {
187     return value >= 0.0f ? 1.0f : -1.0f;
188 }
189 /** @} */
190 }; // namespace Math
191 BASE_END_NAMESPACE()
192 
193 #endif // API_BASE_MATH_MATHF_H
194