27template<
typename T,
typename X,
typename Y>
29 requires(X value, X from, X to, Y outFrom, Y outTo,
bool clamp)
31 { T::mapValue(value, from, to, outFrom, outTo,
clamp) } -> std::convertible_to<Y>;
40template <
typename TFloat,
typename X,
typename Y>
44 constexpr Lerp(X iX1, Y iY1, X iX2, Y iY2) noexcept :
45 fA((
static_cast<TFloat
>(iY1 - iY2)) / (
static_cast<TFloat
>(iX1 - iX2))),
46 fB(
static_cast<TFloat
>(iY1) -
fA *
static_cast<TFloat
>(iX1)) {}
51 constexpr Lerp(Y iY0, Y iY1) noexcept :
fA(
static_cast<TFloat
>(iY1 - iY0)),
fB(
static_cast<TFloat
>(iY0)) {};
55 return static_cast<Y
>((
static_cast<TFloat
>(iX) *
fA) +
fB);
61 return static_cast<X
>((
static_cast<TFloat
>(iY) -
fB) /
fA);
69 static constexpr Lerp mapRange(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
noexcept
71 return Lerp(iFromLow, iToLow, iFromHigh, iToHigh);
89 static constexpr Y
mapValue(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
92 if(iFromLow == iFromHigh)
93 return iValue <= iFromLow ? iToLow : iToHigh;
101 iValue =
clampRange(iValue, iFromLow, iFromHigh);
104 return Lerp(iFromLow, iToLow, iFromHigh, iToHigh).computeY(iValue);
115template<
typename X,
typename Y>
128template<
typename X,
typename Y>
144template<
typename X,
typename Y>
145constexpr static Y
mapValueSPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
153constexpr static Y
mapValueSPY(
float iValue,
float iFromLow,
float iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
161constexpr static float mapValueSPX(X iValue, X iFromLow, X iFromHigh,
float iToLow,
float iToHigh,
bool iClamp =
true)
168constexpr static float mapValueSP(
float iValue,
float iFromLow,
float iFromHigh,
float iToLow,
float iToHigh,
bool iClamp =
true)
170 return SPLerp::mapValue(iValue, iFromLow, iFromHigh, iToLow, iToHigh, iClamp);
177template<
typename X,
typename Y>
190template<
typename X,
typename Y>
206template<
typename X,
typename Y>
207constexpr static Y
mapValueDPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
215constexpr static Y
mapValueDPY(
double iValue,
double iFromLow,
double iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
223constexpr static double mapValueDPX(X iValue, X iFromLow, X iFromHigh,
double iToLow,
double iToHigh,
bool iClamp =
true)
230constexpr static double mapValueDP(
double iValue,
double iFromLow,
double iFromHigh,
double iToLow,
double iToHigh,
bool iClamp =
true)
232 return DPLerp::mapValue(iValue, iFromLow, iFromHigh, iToLow, iToHigh, iClamp);
270 return iValue >=
fFrom && iValue <=
fTo;
272 return iValue <= fFrom && iValue >=
fTo;
293 template<
typename U, HasStaticMapValue<T, U> TLerp = DPLerpXY<T, U>>
296 return TLerp::mapValue(iValue,
fFrom,
fTo, iRange.
fFrom, iRange.
fTo, iClampToRange);
305 template<
typename U, HasStaticMapValue<T, U> TLerp = DPLerpXY<T, U>>
320 template<
typename U, HasStaticMapValue<T, U> TLerp = DPLerpXY<T, U>>
346 return !(rhs == *
this);
Util class to compute linear interpolation.
Definition Lerp.h:42
const TFloat fA
Definition Lerp.h:108
static constexpr Lerp mapRange(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh) noexcept
Inspired by the map function in Processing language, another way to look at Lerp is to map a range of...
Definition Lerp.h:69
constexpr Lerp(Y iY0, Y iY1) noexcept
Shortcut for when x=0 => iY0 and x=1.0 => iY1.
Definition Lerp.h:51
constexpr X computeX(Y iY) const
Definition Lerp.h:58
const TFloat fB
Definition Lerp.h:109
constexpr Lerp(X iX1, Y iY1, X iX2, Y iY2) noexcept
Definition Lerp.h:44
constexpr Y computeY(X iX) const
Definition Lerp.h:53
static constexpr Y mapValue(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Inspired by the map function in Processing language, another way to look at Lerp is to map a range of...
Definition Lerp.h:89
Concept for a static mapValue function.
Definition Lerp.h:28
Definition CircularBuffer.h:27
Lerp< float, X, Y > SPLerpXY
Definition Lerp.h:116
Lerp< double, X, double > DPLerpX
Definition Lerp.h:181
Lerp< double, X, Y > DPLerpXY
Definition Lerp.h:178
static constexpr Y mapValueSPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:145
static constexpr SPLerpXY< X, Y > mapRangeSPXY(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh) noexcept
Convenient shortcut for single precision.
Definition Lerp.h:129
Lerp< double, double, double > DPLerp
Definition Lerp.h:186
static constexpr Y mapValueSPY(float iValue, float iFromLow, float iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:153
constexpr auto mapRangeDPY
Definition Lerp.h:197
static constexpr T clamp(const U &iValue, const T &iLower, const T &iUpper)
Make sure that the value remains within its bounds.
Definition Misc.h:34
constexpr auto mapRangeSPX
Definition Lerp.h:138
Lerp< float, X, float > SPLerpX
Definition Lerp.h:119
Lerp< float, float, float > SPLerp
Definition Lerp.h:124
Lerp< float, float, Y > SPLerpY
Definition Lerp.h:122
static constexpr Y mapValueDPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:207
static constexpr Y mapValueDPY(double iValue, double iFromLow, double iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:215
static constexpr T clampRange(const U &iValue, const T &iFrom, const T &iTo)
Make sure that the value remains within its bounds.
Definition Misc.h:45
constexpr auto mapRangeDPX
Definition Lerp.h:200
constexpr auto mapRangeSPY
Definition Lerp.h:135
static constexpr DPLerpXY< X, Y > mapRangeDPXY(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
Convenient shortcut for double precision.
Definition Lerp.h:191
Lerp< double, double, Y > DPLerpY
Definition Lerp.h:184
static constexpr float mapValueSPX(X iValue, X iFromLow, X iFromHigh, float iToLow, float iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:161
constexpr auto mapRangeDP
Definition Lerp.h:202
constexpr auto mapRangeSP
Definition Lerp.h:140
static constexpr double mapValueDPX(X iValue, X iFromLow, X iFromHigh, double iToLow, double iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:223
static constexpr double mapValueDP(double iValue, double iFromLow, double iFromHigh, double iToLow, double iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:230
static constexpr float mapValueSP(float iValue, float iFromLow, float iFromHigh, float iToLow, float iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:168
constexpr bool isSingleValue() const
Definition Lerp.h:255
constexpr Range()=default
constexpr Range< U > mapRange(Range< U > const &iRange) const
Map this range to the other range.
Definition Lerp.h:306
constexpr Range(T iValue) noexcept
Definition Lerp.h:249
CCoord fFrom
Definition Lerp.h:350
bool operator!=(const Range &rhs) const
Definition Lerp.h:344
T fTo
Definition Lerp.h:351
constexpr U mapValue(T iValue, Range< U > const &iRange, bool iClampToRange=true) const
Map the value from this range into the provide range.
Definition Lerp.h:294
bool operator==(const Range &rhs) const
Definition Lerp.h:338
constexpr Range< U > mapSubRange(Range< T > const &iSubRange, Range< U > const &iRange, bool iClampToRange=true) const
Map a sub range of this range to the other range.
Definition Lerp.h:321
constexpr Range< U > cast() const
Cast this range to another one.
Definition Lerp.h:332
constexpr bool contains(T iValue) const
This method assumes that fFrom and fTo are part of the range or another way to put it:
Definition Lerp.h:267
constexpr Range(T iFrom, T iTo) noexcept
Definition Lerp.h:252
constexpr T clamp(T iValue) const
Clamp the value to this range.
Definition Lerp.h:280
T value_type
Gives access to the type of elements in the range.
Definition Lerp.h:243