1 /*
2  * Copyright (c) 2024 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 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 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 template<typename T>
46 constexpr inline const T& clamp(const T& value, const T& min, const T& max)
47 {
48     return (value < min) ? min : (value > max) ? max : value;
49 }
50 
51 /** Clamps value between 0 and 1 and then returns it */
52 constexpr inline float clamp01(float value)
53 {
54     if (value < 0) {
55         return 0;
56     } else if (value > 1) {
57         return 1;
58     } else {
59         return value;
60     }
61 }
62 
63 /** Returns Absolute value of given float */
64 constexpr inline float abs(float value)
65 {
66     return value < 0 ? -value : value;
67 }
68 
69 /** Returns Absolute value of given integer */
70 constexpr inline int abs(int value)
71 {
72     return value < 0 ? -value : value;
73 }
74 
75 /** Floor the given float value */
76 inline float floor(float value)
77 {
78     return floorf(value);
79 }
80 
81 /** Ceil the given float value */
82 inline float ceil(float value)
83 {
84     return ceilf(value);
85 }
86 
87 /** Returns largest integer smaller to or equal to value */
88 inline int floorToInt(float value)
89 {
90     return static_cast<int>(floor(value));
91 }
92 
93 /** Returns smallest integer greater to or equal to value */
94 inline int ceilToInt(float value)
95 {
96     return static_cast<int>(ceil(value));
97 }
98 
99 /** Interpolates between floats a and b by t and t is clamped value between 0 and 1 */
100 constexpr inline float lerp(float a, float b, float t)
101 {
102     return a + (b - a) * clamp01(t);
103 }
104 
105 /** Returns value rounded to the nearest integer */
106 inline float round(float value)
107 {
108     return roundf(value);
109 }
110 
111 /** Returns square root of value */
112 inline float sqrt(float value)
113 {
114     return sqrtf(value);
115 }
116 
117 /** Returns the sine of angle value in radians */
118 inline float sin(float value)
119 {
120     return sinf(value);
121 }
122 
123 /** Returns the cosine of angle value in radians */
124 inline float cos(float value)
125 {
126     return cosf(value);
127 }
128 
129 /** Returns the tangent of angle value in radians */
130 inline float tan(float value)
131 {
132     return tanf(value);
133 }
134 
135 /** Returns f raised to power p */
136 inline float pow(float f, float p)
137 {
138     return powf(f, p);
139 }
140 
141 /** atan wrapper for float */
142 inline float atan(float f)
143 {
144     return atanf(f);
145 }
146 
147 /** atan2 wrapper for float */
148 inline float atan2(float a, float b)
149 {
150     return atan2f(a, b);
151 }
152 
153 /** asin wrapper for float */
154 inline float asin(float value)
155 {
156     return asinf(value);
157 }
158 
159 /** acos wrapper for float */
160 inline float acos(float value)
161 {
162     return acosf(value);
163 }
164 
165 /** Epsilon (1.192092896e-07F) */
166 static constexpr float EPSILON = BASE_EPSILON;
167 
168 /** Positive infinity */
169 static float infinity = INFINITY;
170 
171 /** 3.14... */
172 static constexpr float PI = 3.141592653f;
173 
174 /** Degrees-to-radians conversion constant */
175 static constexpr float DEG2RAD = PI * 2.0f / 360.0f;
176 
177 /** Radians-to-degrees conversion constant */
178 static constexpr float RAD2DEG = 1.0f / DEG2RAD;
179 
180 /** Returns the sign of value */
181 static constexpr inline float sign(float value)
182 {
183     return value >= 0.0f ? 1.0f : -1.0f;
184 }
185 /** @} */
186 }; // namespace Math
187 BASE_END_NAMESPACE()
188 
189 #endif // API_BASE_MATH_MATHF_H
190