31template <
typename TFloat,
typename X,
typename Y>
35 Lerp(X iX1, Y iY1, X iX2, Y iY2) :
36 fA((static_cast<TFloat>(iY1 - iY2)) / (static_cast<TFloat>(iX1 - iX2))),
37 fB(static_cast<TFloat>(iY1) -
fA * static_cast<TFloat>(iX1)) {}
42 Lerp(Y iY0, Y iY1) :
fA(static_cast<TFloat>(iY1 - iY0)),
fB(static_cast<TFloat>(iY0)) {};
46 return static_cast<Y
>((
static_cast<TFloat
>(iX) *
fA) +
fB);
52 return static_cast<X
>((
static_cast<TFloat
>(iY) -
fB) /
fA);
60 static inline Lerp mapRange(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
62 return Lerp(iFromLow, iToLow, iFromHigh, iToHigh);
80 static Y
mapValue(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
83 if(iFromLow == iFromHigh)
84 return iValue <= iFromLow ? iToLow : iToHigh;
92 iValue =
clampRange(iValue, iFromLow, iFromHigh);
95 return Lerp(iFromLow, iToLow, iFromHigh, iToHigh).computeY(iValue);
106template<
typename X,
typename Y>
119template<
typename X,
typename Y>
135template<
typename X,
typename Y>
136inline static Y
mapValueSPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
144inline static Y
mapValueSPY(
float iValue,
float iFromLow,
float iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
152inline static float mapValueSPX(X iValue, X iFromLow, X iFromHigh,
float iToLow,
float iToHigh,
bool iClamp =
true)
159inline static float mapValueSP(
float iValue,
float iFromLow,
float iFromHigh,
float iToLow,
float iToHigh,
bool iClamp =
true)
161 return SPLerp::mapValue(iValue, iFromLow, iFromHigh, iToLow, iToHigh, iClamp);
168template<
typename X,
typename Y>
181template<
typename X,
typename Y>
197template<
typename X,
typename Y>
198inline static Y
mapValueDPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
206inline static Y
mapValueDPY(
double iValue,
double iFromLow,
double iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
214inline static double mapValueDPX(X iValue, X iFromLow, X iFromHigh,
double iToLow,
double iToHigh,
bool iClamp =
true)
221inline static double mapValueDP(
double iValue,
double iFromLow,
double iFromHigh,
double iToLow,
double iToHigh,
bool iClamp =
true)
223 return DPLerp::mapValue(iValue, iFromLow, iFromHigh, iToLow, iToHigh, iClamp);
261 return iValue >=
fFrom && iValue <=
fTo;
263 return iValue <= fFrom && iValue >=
fTo;
284 template<
typename U,
typename TLerp = DPLerpXY<T, U>>
287 return TLerp::mapValue(iValue,
fFrom,
fTo, iRange.
fFrom, iRange.
fTo, iClampToRange);
296 template<
typename U,
typename TLerp = DPLerpXY<T, U>>
311 template<
typename U,
typename TLerp = DPLerpXY<T, U>>
337 return !(rhs == *
this);
Util class to compute linear interpolation.
Definition Lerp.h:33
const float fA
Definition Lerp.h:99
Lerp(X iX1, Y iY1, X iX2, Y iY2)
Definition Lerp.h:35
static 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:80
Lerp(Y iY0, Y iY1)
Shortcut for when x=0 => iY0 and x=1.0 => iY1.
Definition Lerp.h:42
const float fB
Definition Lerp.h:100
Y computeY(X iX) const
Definition Lerp.h:44
static Lerp mapRange(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
Inspired by the map function in Processing language, another way to look at Lerp is to map a range of...
Definition Lerp.h:60
X computeX(Y iY) const
Definition Lerp.h:49
Definition CircularBuffer.h:26
Lerp< float, X, Y > SPLerpXY
Definition Lerp.h:107
Lerp< double, X, double > DPLerpX
Definition Lerp.h:172
static Y mapValueSPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:136
static float mapValueSP(float iValue, float iFromLow, float iFromHigh, float iToLow, float iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:159
static double mapValueDPX(X iValue, X iFromLow, X iFromHigh, double iToLow, double iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:214
Lerp< double, X, Y > DPLerpXY
Definition Lerp.h:169
Lerp< double, double, double > DPLerp
Definition Lerp.h:177
static double mapValueDP(double iValue, double iFromLow, double iFromHigh, double iToLow, double iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:221
constexpr auto mapRangeDPY
Definition Lerp.h:188
constexpr auto mapRangeSPX
Definition Lerp.h:129
static Y mapValueSPY(float iValue, float iFromLow, float iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:144
Lerp< float, X, float > SPLerpX
Definition Lerp.h:110
Lerp< float, float, float > SPLerp
Definition Lerp.h:115
Lerp< float, float, Y > SPLerpY
Definition Lerp.h:113
constexpr auto mapRangeDPX
Definition Lerp.h:191
constexpr auto mapRangeSPY
Definition Lerp.h:126
Lerp< double, double, Y > DPLerpY
Definition Lerp.h:175
static Y mapValueDPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:198
constexpr auto mapRangeDP
Definition Lerp.h:193
constexpr auto mapRangeSP
Definition Lerp.h:131
static Y mapValueDPY(double iValue, double iFromLow, double iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:206
static T clampRange(const U &iValue, const T &iFrom, const T &iTo)
Make sure that the value remains within its bounds.
Definition Misc.h:45
static float mapValueSPX(X iValue, X iFromLow, X iFromHigh, float iToLow, float iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition Lerp.h:152
static DPLerpXY< X, Y > mapRangeDPXY(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
Convenient shortcut for double precision.
Definition Lerp.h:182
static SPLerpXY< X, Y > mapRangeSPXY(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
Convenient shortcut for single precision.
Definition Lerp.h:120
Range(T iValue) noexcept
Definition Lerp.h:240
bool isSingleValue() const
Definition Lerp.h:246
Range(T iFrom, T iTo)
Definition Lerp.h:243
CCoord fFrom
Definition Lerp.h:341
Range< U > cast() const
Cast this range to another one.
Definition Lerp.h:323
bool operator!=(const Range &rhs) const
Definition Lerp.h:335
Range< U > mapRange(Range< U > const &iRange) const
Map this range to the other range.
Definition Lerp.h:297
T fTo
Definition Lerp.h:342
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:258
bool operator==(const Range &rhs) const
Definition Lerp.h:329
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:285
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:312
T clamp(T iValue) const
Clamp the value to this range.
Definition Lerp.h:271
T value_type
Gives access to the type of elements in the range.
Definition Lerp.h:234