From f519625ed2ab5a88c86dfa6d833059023e2996b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 10 Jul 2014 15:26:35 -0700 Subject: [PATCH] allow specifying a base parameter for exponential functions refs mapbox/mapbox-gl-style-spec#4 --- include/llmr/style/function_properties.hpp | 6 +++--- src/style/function_properties.cpp | 10 +++++----- src/style/style_parser.cpp | 3 ++- test/functions.cpp | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/include/llmr/style/function_properties.hpp b/include/llmr/style/function_properties.hpp index 75714d119..e72ced0b0 100644 --- a/include/llmr/style/function_properties.hpp +++ b/include/llmr/style/function_properties.hpp @@ -29,14 +29,14 @@ private: template struct ExponentialFunction { - inline ExponentialFunction(const T &value, float z_base, float slope, const T &min, + inline ExponentialFunction(const T &value, float z_base, float exp_base, float slope, const T &min, const T &max) - : value(value), min(min), max(max), z_base(z_base), slope(slope) {} + : value(value), min(min), max(max), z_base(z_base), exp_base(exp_base), slope(slope) {} T evaluate(float z) const; private: const T value, min, max; - const float z_base, slope; + const float z_base, exp_base, slope; }; template diff --git a/src/style/function_properties.cpp b/src/style/function_properties.cpp index 98f6503be..9ed04b1b5 100644 --- a/src/style/function_properties.cpp +++ b/src/style/function_properties.cpp @@ -34,16 +34,16 @@ bool ExponentialFunction::evaluate(float z) const { template <> float ExponentialFunction::evaluate(float z) const { - return std::fmin(std::fmax(min, value + std::pow(1.75, (z - z_base)) * slope), max); + return std::fmin(std::fmax(min, value + std::pow(exp_base, (z - z_base)) * slope), max); } template <> Color ExponentialFunction::evaluate(float z) const { return {{ - std::fmin(std::fmax(min[0], value[0] + float(std::pow(1.75, (z - z_base))) * slope), max[0]), - std::fmin(std::fmax(min[1], value[1] + float(std::pow(1.75, (z - z_base))) * slope), max[1]), - std::fmin(std::fmax(min[2], value[2] + float(std::pow(1.75, (z - z_base))) * slope), max[2]), - std::fmin(std::fmax(min[3], value[3] + float(std::pow(1.75, (z - z_base))) * slope), max[3]) + std::fmin(std::fmax(min[0], value[0] + float(std::pow(exp_base, (z - z_base))) * slope), max[0]), + std::fmin(std::fmax(min[1], value[1] + float(std::pow(exp_base, (z - z_base))) * slope), max[1]), + std::fmin(std::fmax(min[2], value[2] + float(std::pow(exp_base, (z - z_base))) * slope), max[2]), + std::fmin(std::fmax(min[3], value[3] + float(std::pow(exp_base, (z - z_base))) * slope), max[3]) }}; } diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index e0b85381b..2b8339a53 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -273,11 +273,12 @@ std::tuple> StyleParser::parseFunction(JSVal value) { return std::tuple> { false, ConstantFunction(T()) }; } const float z_base = parseFunctionArgument(value["z"]); + const float exp_base = value.HasMember("base") ? parseFunctionArgument(value["base"]) : 1.75; const T val = parseFunctionArgument(value["val"]); const float slope = value.HasMember("slope") ? parseFunctionArgument(value["slope"]) : 1; const T min = value.HasMember("min") ? parseFunctionArgument(value["min"]) : T(); const T max = value.HasMember("max") ? parseFunctionArgument(value["max"]) : T(); - return std::tuple> { true, ExponentialFunction(val, z_base, slope, min, max) }; + return std::tuple> { true, ExponentialFunction(val, z_base, exp_base, slope, min, max) }; } else if (type == "stops") { if (!value.HasMember("stops")) { diff --git a/test/functions.cpp b/test/functions.cpp index db75efec4..dbaeaeee8 100644 --- a/test/functions.cpp +++ b/test/functions.cpp @@ -63,7 +63,7 @@ TEST(Function, Linear) { TEST(Function, Exponential) { - llmr::ExponentialFunction slope_1(/* val */ 7.5, /* z_base */ 4, /* slope */ 2, /* min */ 7.5, /* max */ 20); + llmr::ExponentialFunction slope_1(/* val */ 7.5, /* z_base */ 4, /* exp_base */ 1.75, /* slope */ 2, /* min */ 7.5, /* max */ 20); ASSERT_FLOAT_EQ(8.6428576, slope_1.evaluate(3)); // 7.5 + 1.75^(3 - 4) * 2 ASSERT_FLOAT_EQ(9.5, slope_1.evaluate(4)); // 7.5 + 1.75^(4 - 4) * 2 ASSERT_FLOAT_EQ(10.145751, slope_1.evaluate(4.5)); // 7.5 + 1.75^(4.5 - 4) * 2