30 template <
typename TFloat,
typename X,
typename Y>
34 Lerp(X iX1, Y iY1, X iX2, Y iY2) :
35 fA((static_cast<TFloat>(iY1 - iY2)) / (static_cast<TFloat>(iX1 - iX2))),
36 fB(static_cast<TFloat>(iY1) -
fA * static_cast<TFloat>(iX1)) {}
41 Lerp(Y iY0, Y iY1) :
fA(static_cast<TFloat>(iY1 - iY0)),
fB(static_cast<TFloat>(iY0)) {};
45 return static_cast<Y>((static_cast<TFloat>(iX) *
fA) +
fB);
51 return static_cast<X>((static_cast<TFloat>(iY) -
fB) /
fA);
59 static inline Lerp mapRange(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
61 return Lerp(iFromLow, iToLow, iFromHigh, iToHigh);
79 static Y
mapValue(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
82 if(iFromLow == iFromHigh)
83 return iValue <= iFromLow ? iToLow : iToHigh;
91 iValue =
clampRange(iValue, iFromLow, iFromHigh);
94 return Lerp(iFromLow, iToLow, iFromHigh, iToHigh).computeY(iValue);
105 template<
typename X,
typename Y>
118 template<
typename X,
typename Y>
134 template<
typename X,
typename Y>
135 inline static Y
mapValueSPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
143 inline static Y
mapValueSPY(
float iValue,
float iFromLow,
float iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
151 inline static float mapValueSPX(X iValue, X iFromLow, X iFromHigh,
float iToLow,
float iToHigh,
bool iClamp =
true)
158 inline static float mapValueSP(
float iValue,
float iFromLow,
float iFromHigh,
float iToLow,
float iToHigh,
bool iClamp =
true)
160 return SPLerp::mapValue(iValue, iFromLow, iFromHigh, iToLow, iToHigh, iClamp);
167 template<
typename X,
typename Y>
180 template<
typename X,
typename Y>
196 template<
typename X,
typename Y>
197 inline static Y
mapValueDPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
205 inline static Y
mapValueDPY(
double iValue,
double iFromLow,
double iFromHigh, Y iToLow, Y iToHigh,
bool iClamp =
true)
213 inline static double mapValueDPX(X iValue, X iFromLow, X iFromHigh,
double iToLow,
double iToHigh,
bool iClamp =
true)
220 inline static double mapValueDP(
double iValue,
double iFromLow,
double iFromHigh,
double iToLow,
double iToHigh,
bool iClamp =
true)
222 return DPLerp::mapValue(iValue, iFromLow, iFromHigh, iToLow, iToHigh, iClamp);
260 return iValue >=
fFrom && iValue <=
fTo;
262 return iValue <= fFrom && iValue >=
fTo;
283 template<
typename U,
typename TLerp = DPLerpXY<T, U>>
286 return TLerp::mapValue(iValue,
fFrom,
fTo, iRange.
fFrom, iRange.
fTo, iClampToRange);
295 template<
typename U,
typename TLerp = DPLerpXY<T, U>>
299 return mapSubRange<U,TLerp>(*
this, iRange,
true);
310 template<
typename U,
typename TLerp = DPLerpXY<T, U>>
314 return Range<U>{mapValue<U,TLerp>(iSubRange.
fFrom, iRange, iClampToRange),
315 mapValue<U,TLerp>(iSubRange.
fTo, iRange, iClampToRange)};
336 return !(rhs == *
this);
T clamp(T iValue) const
Clamp the value to this range.
Definition: Lerp.h:270
static SPLerpXY< X, Y > mapRangeSPXY(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
Convenient shortcut for single precision.
Definition: Lerp.h:119
Defines a range of values.
Definition: Lerp.h:229
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:79
static double mapValueDP(double iValue, double iFromLow, double iFromHigh, double iToLow, double iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:220
T fTo
Definition: Lerp.h:341
bool operator!=(const Range &rhs) const
Definition: Lerp.h:334
bool operator==(const Range &rhs) const
Definition: Lerp.h:328
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:59
Definition: CircularBuffer.h:25
static DPLerpXY< X, Y > mapRangeDPXY(X iFromLow, X iFromHigh, Y iToLow, Y iToHigh)
Convenient shortcut for double precision.
Definition: Lerp.h:181
bool isSingleValue() const
Definition: Lerp.h:245
static T clampRange(const U &iValue, const T &iFrom, const T &iTo)
Make sure that the value remains within its bounds.
Definition: Misc.h:44
constexpr auto mapRangeSPY
Definition: Lerp.h:125
Lerp(Y iY0, Y iY1)
Shortcut for when x=0 => iY0 and x=1.0 => iY1.
Definition: Lerp.h:41
static float mapValueSP(float iValue, float iFromLow, float iFromHigh, float iToLow, float iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:158
Range< U > cast() const
Cast this range to another one.
Definition: Lerp.h:322
static double mapValueDPX(X iValue, X iFromLow, X iFromHigh, double iToLow, double iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:213
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:311
const TFloat fA
Definition: Lerp.h:98
constexpr auto mapRangeSP
Definition: Lerp.h:130
static Y mapValueDPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:197
constexpr auto mapRangeDP
Definition: Lerp.h:192
CCoord value_type
Gives access to the type of elements in the range.
Definition: Lerp.h:233
Util class to compute linear interpolation.
Definition: Lerp.h:31
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:257
static Y mapValueDPY(double iValue, double iFromLow, double iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:205
T fFrom
Definition: Lerp.h:340
static Y mapValueSPXY(X iValue, X iFromLow, X iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:135
constexpr auto mapRangeSPX
Definition: Lerp.h:128
Lerp(X iX1, Y iY1, X iX2, Y iY2)
Definition: Lerp.h:34
static float mapValueSPX(X iValue, X iFromLow, X iFromHigh, float iToLow, float iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:151
Range(T iFrom, T iTo)
Definition: Lerp.h:242
Range(T iValue) noexcept
Definition: Lerp.h:239
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:284
Y computeY(X iX) const
Definition: Lerp.h:43
X computeX(Y iY) const
Definition: Lerp.h:48
static Y mapValueSPY(float iValue, float iFromLow, float iFromHigh, Y iToLow, Y iToHigh, bool iClamp=true)
Convenient shortcut for single precision.
Definition: Lerp.h:143
Range< U > mapRange(Range< U > const &iRange) const
Map this range to the other range.
Definition: Lerp.h:296
constexpr auto mapRangeDPX
Definition: Lerp.h:190
const TFloat fB
Definition: Lerp.h:99
constexpr auto mapRangeDPY
Definition: Lerp.h:187