n/lerp: use DEBUG_LIMIT, comments, style

This commit is contained in:
Danny Robson 2015-05-29 15:51:42 +10:00
parent 043523a794
commit 3a8179dfff

View File

@ -27,7 +27,8 @@ template <typename T>
T
util::lerp::sigmoid (T val)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
static_assert (std::is_floating_point<T>::value,
"lerp is only defined for floating types");
return -1 + 2 / (1 + std::exp (-2 * val));
}
@ -38,9 +39,12 @@ template double util::lerp::sigmoid (double);
//-----------------------------------------------------------------------------
template <typename T>
T
util::lerp::trunc (T a, T, T)
util::lerp::trunc (T a, T, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
static_assert (std::is_floating_point<T>::value,
"lerp is only defined for floating types");
CHECK_LIMIT (weight, 0, 1);
return a;
}
@ -53,8 +57,10 @@ template <typename T>
T
util::lerp::linear (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
static_assert (std::is_floating_point<T>::value,
"lerp is only defined for floating types");
CHECK_LIMIT (weight, 0, 1);
return a * (1 - weight) + b * weight;
}
@ -67,10 +73,11 @@ template <typename T>
T
util::lerp::cosine (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
T t = (1 - std::cos (weight * PI<T>)) * T(0.5);
static_assert (std::is_floating_point<T>::value,
"lerp is only defined for floating types");
CHECK_LIMIT (weight, 0, 1);
T t = (1 - std::cos (weight * PI<T>)) * T(0.5);
return a * (1 - t) + b * t;
}
@ -83,8 +90,11 @@ template <typename T>
T
util::lerp::cubic (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
CHECK (weight >= 0 && weight <= 1);
static_assert (std::is_floating_point<T>::value,
"lerp is only defined for floating types");
CHECK_LIMIT (weight, 0, 1);
// -2w^3 * 3w^2
T t = weight * weight * (3 - 2 * weight);
return a * (1 - t) + b * t;
}
@ -98,9 +108,11 @@ template <typename T>
T
util::lerp::quintic (T a, T b, T weight)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
static_assert (std::is_floating_point<T>::value,
"lerp is only defined for floating types");
CHECK_LIMIT (weight, 0, 1);
CHECK (weight >= 0 && weight <= 1);
// from perlin's improved noise: 6w^5 -15w^4 +10w^3
T t = weight * weight * weight * (weight * (weight * 6 - 15) + 10);
return a * (1 - t) + b * t;
}