From 3a8179dffffc4edf4835b79d761c5ee1fb8251b6 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 29 May 2015 15:51:42 +1000 Subject: [PATCH] n/lerp: use DEBUG_LIMIT, comments, style --- noise/lerp.cpp | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/noise/lerp.cpp b/noise/lerp.cpp index 84b7b5d1..a204b67f 100644 --- a/noise/lerp.cpp +++ b/noise/lerp.cpp @@ -27,24 +27,28 @@ template T util::lerp::sigmoid (T val) { - static_assert (std::is_floating_point::value, "lerp is only defined for floating types"); + 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 float util::lerp::sigmoid (float); template double util::lerp::sigmoid (double); //----------------------------------------------------------------------------- template T -util::lerp::trunc (T a, T, T) +util::lerp::trunc (T a, T, T weight) { - static_assert (std::is_floating_point::value, "lerp is only defined for floating types"); + static_assert (std::is_floating_point::value, + "lerp is only defined for floating types"); + CHECK_LIMIT (weight, 0, 1); + return a; } -template float util::lerp::trunc (float, float, float); +template float util::lerp::trunc (float, float, float); template double util::lerp::trunc (double, double, double); @@ -53,12 +57,14 @@ 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); + static_assert (std::is_floating_point::value, + "lerp is only defined for floating types"); + CHECK_LIMIT (weight, 0, 1); + return a * (1 - weight) + b * weight; } -template float util::lerp::linear (float, float, float); +template float util::lerp::linear (float, float, float); template double util::lerp::linear (double, double, double); @@ -67,14 +73,15 @@ 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); + static_assert (std::is_floating_point::value, + "lerp is only defined for floating types"); + CHECK_LIMIT (weight, 0, 1); + T t = (1 - std::cos (weight * PI)) * T(0.5); return a * (1 - t) + b * t; } -template float util::lerp::cosine (float, float, float); +template float util::lerp::cosine (float, float, float); template double util::lerp::cosine (double, double, double); @@ -83,13 +90,16 @@ 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); + static_assert (std::is_floating_point::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; } -template float util::lerp::cubic (float, float, float); +template float util::lerp::cubic (float, float, float); template double util::lerp::cubic (double, double, double); @@ -98,13 +108,15 @@ 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"); + static_assert (std::is_floating_point::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; } -template float util::lerp::quintic (float, float, float); +template float util::lerp::quintic (float, float, float); template double util::lerp::quintic (double, double, double);