Skip to content

Commit

Permalink
fix function
Browse files Browse the repository at this point in the history
  • Loading branch information
SachiSakurane committed Dec 21, 2024
1 parent 006a673 commit 1653ffc
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions include/riw/value_range/functions.hpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
#pragma once

#include <concepts>

#include <riw/algorithm/lerp.hpp>
#include <riw/value_range/value_range.hpp>
#include <riw/concepts/arithmetic.hpp>
#include <riw/concepts/floating_point.hpp>

namespace riw {
template <riw::arithmetic Type>
template <std::totally_ordered Type>
inline constexpr Type length(const value_range<Type> &r) {
return r.max - r.min;
}

template <riw::arithmetic Type>
template <std::totally_ordered Type>
inline constexpr Type clamp(Type value, const value_range<Type> &r) {
return std::clamp(value, r.min, r.max);
}

template <riw::floating_point Type>
template <std::floating_point Type>
inline constexpr Type proportion(Type value, const value_range<Type> &r) {
return (value - r.min) / length(r);
}

template <riw::floating_point Type>
template <std::floating_point Type>
inline constexpr bool contain(Type value, const value_range<Type> &r) {
return r.min <= value && value <= r.max;
}

template <riw::floating_point FloatType>
template <std::floating_point FloatType>
inline constexpr FloatType lerp(const value_range<FloatType> &range, FloatType x) {
return range.min * (static_cast<FloatType>(1) - x) + range.max * x;
}

template <riw::floating_point Type>
template <std::floating_point Type>
inline constexpr Type normalize(Type v, const riw::value_range<Type> &from,
const riw::value_range<Type> &to = {static_cast<Type>(0),
static_cast<Type>(1)}) {
return riw::lerp(to, riw::proportion(v, from));
}

template <riw::floating_point Type>
template <std::floating_point Type>
inline constexpr decltype(auto) inverse(Type v,
riw::value_range<Type> range = riw::normal_range<Type>) {
return range.max + range.min - v;
Expand All @@ -48,7 +50,7 @@ inline constexpr decltype(auto) convert(const auto &range) {
return riw::value_range<To>{static_cast<To>(range.min), static_cast<To>(range.max)};
}

template <riw::floating_point Type>
template <std::floating_point Type>
inline constexpr decltype(auto) log10(const value_range<Type> &r) {
assert(r.min >= 0);
return riw::value_range{std::log10(r.min), std::log10(r.max)};
Expand Down

0 comments on commit 1653ffc

Please sign in to comment.