From ebbc90c88d07eddd40842b570fcd04d6b55b869b Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 18 May 2015 14:55:20 +1000 Subject: [PATCH] noise: templatise lerp functions --- noise/lerp.cpp | 88 ++++++++++++++++++++++++++++++++++++-------------- noise/lerp.hpp | 12 +++---- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/noise/lerp.cpp b/noise/lerp.cpp index a9a646ec..84b7b5d1 100644 --- a/noise/lerp.cpp +++ b/noise/lerp.cpp @@ -23,48 +23,88 @@ //----------------------------------------------------------------------------- -double -util::lerp::sigmoid (double val) { - return -1.0 + 2.0 / (1.0 + exp (-2.0 * val)); +template +T +util::lerp::sigmoid (T val) +{ + static_assert (std::is_floating_point::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 +T +util::lerp::trunc (T a, T, T) +{ + static_assert (std::is_floating_point::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 +T +util::lerp::linear (T a, T b, T weight) +{ + static_assert (std::is_floating_point::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)) * 0.5; +template +T +util::lerp::cosine (T a, T b, T weight) +{ + static_assert (std::is_floating_point::value, "lerp is only defined for floating types"); + CHECK (weight >= 0 && weight <= 1); + T t = (1 - std::cos (weight * PI)) * 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 +T +util::lerp::cubic (T a, T b, T weight) +{ + static_assert (std::is_floating_point::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 +T +util::lerp::quintic (T a, T b, T weight) +{ + static_assert (std::is_floating_point::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); diff --git a/noise/lerp.hpp b/noise/lerp.hpp index 48d01b5a..618f6711 100644 --- a/noise/lerp.hpp +++ b/noise/lerp.hpp @@ -18,13 +18,13 @@ #define __UTIL_NOISE_LERP_HPP namespace util { namespace lerp { - double sigmoid (double val); + template 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 T linear (T a, T b, T weight); + template T cosine (T a, T b, T weight); + template T cubic (T a, T b, T weight); + template T quintic (T a, T b, T weight); + template T trunc (T a, T b, T weight); } } #endif