40 lines
1023 B
C++
40 lines
1023 B
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <type_traits>
|
|
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
|
|
namespace utils {
|
|
|
|
template<typename InType, typename OutType>
|
|
auto interpolate(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
|
|
{
|
|
return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
|
|
}
|
|
|
|
template<typename InType, typename OutType>
|
|
auto map(InType value, InType inMin, InType inMax, OutType outMin, OutType outMax)
|
|
{
|
|
if constexpr(std::is_integral_v<OutType>) {
|
|
return std::clamp(static_cast<OutType>(std::round(interpolate(value, inMin, inMax, outMin, outMax))), outMin, outMax);
|
|
}
|
|
|
|
return std::clamp(static_cast<OutType>(interpolate(value, inMin, inMax, outMin, outMax)), outMin, outMax);
|
|
}
|
|
|
|
template<typename>
|
|
struct array_size;
|
|
|
|
template<typename T, size_t N>
|
|
struct array_size<std::array<T, N>> {
|
|
static constexpr auto value = N;
|
|
};
|
|
|
|
template<typename T>
|
|
inline constexpr auto array_size_v = array_size<T>::value;
|
|
|
|
} // namespace utils
|