From e53c71543aaab8c82ffef377bc55c5563ff8e165 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Sun, 28 Apr 2013 15:43:02 +0200 Subject: [PATCH] Move and rework pow to math/SGMisc --- simgear/math/SGMisc.hxx | 16 +++++++++++ simgear/props/easing_functions.cxx | 46 +++++++++++------------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/simgear/math/SGMisc.hxx b/simgear/math/SGMisc.hxx index 90f9c663..0f57edf1 100644 --- a/simgear/math/SGMisc.hxx +++ b/simgear/math/SGMisc.hxx @@ -58,6 +58,22 @@ public: return var = max(var - rate * dt, target); } + /** + * Get @c base raised to the power of @c N + * + * @tparam N Exponent + * @param base Base + */ + template + static T pow(T base) + { + return (N < 0) + ? (1. / pow<-N>(base)) + : ( ((N & 1) ? base : 1) + * ((N > 1) ? pow(base * base) : 1) + ); + } + static int sign(const T& a) { if (a < -SGLimits::min()) diff --git a/simgear/props/easing_functions.cxx b/simgear/props/easing_functions.cxx index 1a8320ef..2548be71 100644 --- a/simgear/props/easing_functions.cxx +++ b/simgear/props/easing_functions.cxx @@ -21,23 +21,11 @@ // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA #include "easing_functions.hxx" -#include -#include +#include + namespace simgear { - // TODO move somewhere to math - template - double pow(double base) - { - return base * pow(base); - } - - template<> - double pow<0>(double) - { - return 1.0; - } /// Simple linear easing. double easingLinear(double t) @@ -82,7 +70,7 @@ namespace simgear { static double calc(double t) { - return pow(t - 1) + 1; + return SGMiscd::pow(t - 1) + 1; } }; @@ -93,7 +81,7 @@ namespace simgear { static double calc(double t) { - return -pow(t - 1) + 1; + return -SGMiscd::pow(t - 1) + 1; } }; @@ -114,7 +102,7 @@ namespace simgear template double easeInOutPow(double t) { - return easeInOut<&pow, &easeOutPow >(t); + return easeInOut<&SGMiscd::pow, &easeOutPow >(t); } /// http://easings.net/#easeInExpo @@ -132,13 +120,13 @@ namespace simgear /// http://easings.net/#easeInCirc double easeInCirc(double t) { - return 1 - std::sqrt(1 - pow<2>(t)); + return 1 - std::sqrt(1 - SGMiscd::pow<2>(t)); } /// http://easings.net/#easeOutCirc double easeOutCirc(double t) { - return std::sqrt(1 - pow<2>(t - 1)); + return std::sqrt(1 - SGMiscd::pow<2>(t - 1)); } static const double ease_s = 1.70158; @@ -147,14 +135,14 @@ namespace simgear double easeInBack(double t) { - return pow<2>(t) * ((ease_s + 1) * t - ease_s); + return SGMiscd::pow<2>(t) * ((ease_s + 1) * t - ease_s); } /// http://easings.net/#easeOutBack double easeOutBack(double t) { t -= 1; - return pow<2>(t) * ((ease_s + 1) * t + ease_s) + 1; + return SGMiscd::pow<2>(t) * ((ease_s + 1) * t + ease_s) + 1; } /// http://easings.net/#easeOutBack @@ -190,13 +178,13 @@ namespace simgear double easeOutBounce(double t) { if( t < 1/2.75 ) - return 7.5625 * pow<2>(t); + return 7.5625 * SGMiscd::pow<2>(t); else if( t < 2/2.75 ) - return 7.5625 * pow<2>(t - 1.5/2.75) + .75; + return 7.5625 * SGMiscd::pow<2>(t - 1.5/2.75) + .75; else if( t < 2.5/2.75 ) - return 7.5625 * pow<2>(t - 2.25/2.75) + .9375; + return 7.5625 * SGMiscd::pow<2>(t - 2.25/2.75) + .9375; else - return 7.5625 * pow<2>(t - 2.625/2.75) + .984375; + return 7.5625 * SGMiscd::pow<2>(t - 2.625/2.75) + .984375; } /// http://easings.net/#easeInBounce @@ -216,10 +204,10 @@ namespace simgear {"linear", &easingLinear}, {"swing", &easeInOutSine}, SG_ADD_EASING_IN_OUT(Sine) - {"easeInQuad", &pow<2>}, - {"easeInCubic", &pow<3>}, - {"easeInQuart", &pow<4>}, - {"easeInQuint", &pow<5>}, + {"easeInQuad", &SGMiscd::pow<2>}, + {"easeInCubic", &SGMiscd::pow<3>}, + {"easeInQuart", &SGMiscd::pow<4>}, + {"easeInQuint", &SGMiscd::pow<5>}, {"easeOutQuad", &easeOutPow<2>}, {"easeOutCubic", &easeOutPow<3>}, {"easeOutQuart", &easeOutPow<4>}, -- 2.39.5