noise: templatise lerp functions

This commit is contained in:
Danny Robson 2015-05-18 14:55:20 +10:00
parent 47423da841
commit ebbc90c88d
2 changed files with 70 additions and 30 deletions

View File

@ -23,48 +23,88 @@
//-----------------------------------------------------------------------------
double
util::lerp::sigmoid (double val) {
return -1.0 + 2.0 / (1.0 + exp (-2.0 * val));
template <typename T>
T
util::lerp::sigmoid (T val)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
return -1 + 2 / (1 + std::exp (-2 * val));
}
template float util::lerp::sigmoid (float);
template double util::lerp::sigmoid (double);
//-----------------------------------------------------------------------------
double util::lerp::trunc (double a, double, double)
{ return a; }
template <typename T>
T
util::lerp::trunc (T a, T, T)
{
static_assert (std::is_floating_point<T>::value, "lerp is only defined for floating types");
return a;
}
template float util::lerp::trunc (float, float, float);
template double util::lerp::trunc (double, double, double);
//-----------------------------------------------------------------------------
double
util::lerp::linear (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0);
return a * (1.0 - weight) + b * weight;
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);
return a * (1 - weight) + b * weight;
}
template float util::lerp::linear (float, float, float);
template double util::lerp::linear (double, double, double);
//-----------------------------------------------------------------------------
double
util::lerp::cosine (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0);
double t = (1.0 - cos (weight * PI<double>)) * 0.5;
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);
return a * (1.0 - t) + b * t;
return a * (1 - t) + b * t;
}
template float util::lerp::cosine (float, float, float);
template double util::lerp::cosine (double, double, double);
//-----------------------------------------------------------------------------
double
util::lerp::cubic (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0);
double t = weight * weight * (3.0 - 2.0 * weight);
return a * (1.0 - t) + b * t;
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);
T t = weight * weight * (3 - 2 * weight);
return a * (1 - t) + b * t;
}
template float util::lerp::cubic (float, float, float);
template double util::lerp::cubic (double, double, double);
//-----------------------------------------------------------------------------
double
util::lerp::quintic (double a, double b, double weight) {
CHECK (weight >= 0.0 && weight <= 1.0);
double t = weight * weight * weight * (weight * (weight * 6.0 - 15.0) + 10.0);
return a * (1.0 - t) + b * t;
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");
CHECK (weight >= 0 && weight <= 1);
T t = weight * weight * weight * (weight * (weight * 6 - 15) + 10);
return a * (1 - t) + b * t;
}
template float util::lerp::quintic (float, float, float);
template double util::lerp::quintic (double, double, double);

View File

@ -18,13 +18,13 @@
#define __UTIL_NOISE_LERP_HPP
namespace util { namespace lerp {
double sigmoid (double val);
template <typename T> T sigmoid (T val);
double linear (double a, double b, double weight);
double cosine (double a, double b, double weight);
double cubic (double a, double b, double weight);
double quintic (double a, double b, double weight);
double trunc (double a, double b, double weight);
template <typename T> T linear (T a, T b, T weight);
template <typename T> T cosine (T a, T b, T weight);
template <typename T> T cubic (T a, T b, T weight);
template <typename T> T quintic (T a, T b, T weight);
template <typename T> T trunc (T a, T b, T weight);
} }
#endif