* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef API_BASE_MATH_MATHF_H
#define API_BASE_MATH_MATHF_H
#include <climits>
#include <cmath>
#include <base/namespace.h>
BASE_BEGIN_NAMESPACE()
namespace Math {
constexpr const float BASE_EPSILON = 1.192092896e-07f;
* @{
*/
template <typename T>
constexpr inline const T& min(const T& a, const T& b) noexcept
{
return a < b ? a : b;
}
template <typename T>
constexpr inline const T& max(const T& a, const T& b) noexcept
{
return a > b ? a : b;
}
template <typename T>
constexpr inline const T& clamp(const T& value, const T& min, const T& max)
{
return (value < min) ? min : (value > max) ? max : value;
}
constexpr inline float clamp01(float value)
{
if (value < 0) {
return 0;
} else if (value > 1) {
return 1;
} else {
return value;
}
}
constexpr inline float abs(float value)
{
return value < 0 ? -value : value;
}
constexpr inline int abs(int value)
{
return value < 0 ? -value : value;
}
inline float floor(float value)
{
return floorf(value);
}
inline float ceil(float value)
{
return ceilf(value);
}
inline int floorToInt(float value)
{
const float clamped = clamp(floor(value), static_cast<float>(INT_MIN), static_cast<float>(INT_MAX));
return static_cast<int>(clamped);
}
inline int ceilToInt(float value)
{
const float clamped = clamp(ceil(value), static_cast<float>(INT_MIN), static_cast<float>(INT_MAX));
return static_cast<int>(clamped);
}
constexpr inline float lerp(float a, float b, float t)
{
return a + (b - a) * clamp01(t);
}
inline float round(float value)
{
return roundf(value);
}
inline float sqrt(float value)
{
return sqrtf(value);
}
inline float cbrt(float value)
{
return cbrtf(value);
}
inline float sin(float value)
{
return sinf(value);
}
inline float cos(float value)
{
return cosf(value);
}
inline float tan(float value)
{
return tanf(value);
}
inline float pow(float f, float p)
{
return powf(f, p);
}
inline float atan(float f)
{
return atanf(f);
}
inline float atan2(float a, float b)
{
return atan2f(a, b);
}
inline float asin(float value)
{
return asinf(value);
}
inline float acos(float value)
{
return acosf(value);
}
static constexpr float EPSILON = BASE_EPSILON;
static float infinity = INFINITY;
static constexpr float PI = 3.141592653f;
static constexpr float DEG2RAD = PI * 2.0f / 360.0f;
static constexpr float RAD2DEG = 1.0f / DEG2RAD;
static constexpr inline float sign(float value)
{
return value >= 0.0f ? 1.0f : -1.0f;
}
};
BASE_END_NAMESPACE()
#endif