From 68f595a32bc746d958490c91aa7e165048491c37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Thu, 17 Jul 2014 14:32:35 -0700 Subject: [PATCH] consolidate functions to only use stops refs mapbox/mapbox-gl-style-spec#96 --- include/mbgl/style/function_properties.hpp | 28 +---- src/style/function_properties.cpp | 87 +++++----------- src/style/style_parser.cpp | 113 +++++++-------------- test/fixtures/styles/road-width.style.json | 1 - test/functions.cpp | 48 +++------ 5 files changed, 77 insertions(+), 200 deletions(-) diff --git a/include/mbgl/style/function_properties.hpp b/include/mbgl/style/function_properties.hpp index 74ac80f83..8cd7ce6e2 100644 --- a/include/mbgl/style/function_properties.hpp +++ b/include/mbgl/style/function_properties.hpp @@ -16,44 +16,20 @@ private: const T value; }; -template -struct LinearFunction { - inline LinearFunction(const T &value, float z_base, float slope, const T &min, const T &max) - : value(value), min(min), max(max), z_base(z_base), slope(slope) {} - T evaluate(float z) const; - -private: - const T value, min, max; - const float z_base, slope; -}; - -template -struct ExponentialFunction { - 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), exp_base(exp_base), slope(slope) {} - T evaluate(float z) const; - -private: - const T value, min, max; - const float z_base, exp_base, slope; -}; - template struct StopsFunction { - inline StopsFunction(const std::vector> &values) : values(values) {} + inline StopsFunction(const std::vector> &values, float base) : values(values), base(base) {} T evaluate(float z) const; private: const std::vector> values; + const float base; }; template using Function = util::variant< std::false_type, ConstantFunction, - LinearFunction, - ExponentialFunction, StopsFunction >; diff --git a/src/style/function_properties.cpp b/src/style/function_properties.cpp index 2b378fb6b..879de84f8 100644 --- a/src/style/function_properties.cpp +++ b/src/style/function_properties.cpp @@ -6,82 +6,36 @@ namespace mbgl { -template <> -bool LinearFunction::evaluate(float z) const { - return z < z_base ? slope >= 0 : z > z_base ? slope >= 0 : value; -} - -template <> -float LinearFunction::evaluate(float z) const { - return std::fmin(std::fmax(min, value + (z - z_base) * slope), max); -} - -template <> -Color LinearFunction::evaluate(float z) const { - return {{ - std::fmin(std::fmax(min[0], value[0] + (z - z_base) * slope), max[0]), - std::fmin(std::fmax(min[1], value[1] + (z - z_base) * slope), max[1]), - std::fmin(std::fmax(min[2], value[2] + (z - z_base) * slope), max[2]), - std::fmin(std::fmax(min[3], value[3] + (z - z_base) * slope), max[3]) - }}; -} - - -template <> -bool ExponentialFunction::evaluate(float z) const { - return z < z_base ? slope >= 0 : z > z_base ? slope >= 0 : value; -} - -template <> -float ExponentialFunction::evaluate(float z) const { - 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(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]) - }}; -} - template -inline T exponentialInterpolate(T smaller, T larger, const float factor); +inline T interpolate(T smaller, T larger, const float factor); template <> -inline float exponentialInterpolate(const float smaller, const float larger, const float factor) { - // Linear interpolation if base is 0 - if (smaller == 0.0f) { - return factor * larger; - } - // Exponential interpolation between the values - return smaller * std::pow(larger / smaller, factor); +inline float interpolate(const float smaller, const float larger, const float factor) { + return (smaller * (1 - factor)) + (larger * factor); } template <> -inline bool exponentialInterpolate(const bool smaller, const bool larger, const float factor) { - return exponentialInterpolate(float(smaller), float(larger), factor); +inline bool interpolate(const bool smaller, const bool larger, const float factor) { + return interpolate(float(smaller), float(larger), factor); } template <> -inline Color exponentialInterpolate(const Color smaller, const Color larger, const float factor) { +inline Color interpolate(const Color smaller, const Color larger, const float factor) { return {{ - exponentialInterpolate(smaller[0], larger[0], factor), - exponentialInterpolate(smaller[1], larger[1], factor), - exponentialInterpolate(smaller[2], larger[2], factor), - exponentialInterpolate(smaller[3], larger[3], factor) + interpolate(smaller[0], larger[0], factor), + interpolate(smaller[1], larger[1], factor), + interpolate(smaller[2], larger[2], factor), + interpolate(smaller[3], larger[3], factor) }}; } template -T exponentialDefault(); +inline T defaultStopsValue(); -template <> bool exponentialDefault() { return true; } -template <> float exponentialDefault() { return 1.0f; } -template <> Color exponentialDefault() { return {{ 0, 0, 0, 1 }}; } +template <> inline bool defaultStopsValue() { return true; } +template <> inline float defaultStopsValue() { return 1.0f; } +template <> inline Color defaultStopsValue() { return {{ 0, 0, 0, 1 }}; } template @@ -112,15 +66,22 @@ T StopsFunction::evaluate(float z) const { if (larger_z == smaller_z || larger_val == smaller_val) { return smaller_val; } - float factor = (z - smaller_z) / (larger_z - smaller_z); - return exponentialInterpolate(smaller_val, larger_val, factor); + const float zoomDiff = larger_z - smaller_z; + const float zoomProgress = z - smaller_z; + if (base == 1.0f) { + const float t = zoomProgress / zoomDiff; + return interpolate(smaller_val, larger_val, t); + } else { + const float t = (std::pow(base, zoomProgress) - 1) / (std::pow(base, zoomDiff) - 1); + return interpolate(smaller_val, larger_val, t); + } } else if (larger) { return larger_val; } else if (smaller) { return smaller_val; } else { // No stop defined. - return exponentialDefault(); + return defaultStopsValue(); } } diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index faa8caeb9..3c79ee3c9 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -230,95 +230,56 @@ Color StyleParser::parseFunctionArgument(JSVal value) { return parseColor(rvalue); } +template inline float defaultBaseValue() { return 1.75; } +template <> inline float defaultBaseValue() { return 1.0; } + template std::tuple> StyleParser::parseFunction(JSVal value) { - if (!value.HasMember("fn")) { - fprintf(stderr, "[WARNING] function must specify a function name\n"); + if (!value.HasMember("stops")) { + fprintf(stderr, "[WARNING] stops function must specify a stops array\n"); return std::tuple> { false, ConstantFunction(T()) }; } - JSVal value_fn = value["fn"]; - if (!value_fn.IsString()) { - fprintf(stderr, "[WARNING] function must specify a function type\n"); + float base = defaultBaseValue(); + + if (value.HasMember("base")) { + JSVal value_base = value["base"]; + if (value_base.IsNumber()) { + base = value_base.GetDouble(); + } else { + fprintf(stderr, "[WARNING] base must be numeric\n"); + } + } + + JSVal value_stops = value["stops"]; + if (!value_stops.IsArray()) { + fprintf(stderr, "[WARNING] stops function must specify a stops array\n"); return std::tuple> { false, ConstantFunction(T()) }; } - const std::string type { value_fn.GetString(), value_fn.GetStringLength() }; - - if (type == "linear") { - if (!value.HasMember("z")) { - fprintf(stderr, "[WARNING] linear function must specify a base zoom level\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - if (!value.HasMember("val")) { - fprintf(stderr, "[WARNING] linear function must specify a base value\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - const float z_base = parseFunctionArgument(value["z"]); - 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, LinearFunction(val, z_base, slope, min, max) }; - } - else if (type == "exponential") { - if (!value.HasMember("z")) { - fprintf(stderr, "[WARNING] exponential function must specify a base zoom level\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - if (!value.HasMember("val")) { - fprintf(stderr, "[WARNING] exponential function must specify a base value\n"); - 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, exp_base, slope, min, max) }; - } - else if (type == "stops") { - if (!value.HasMember("stops")) { - fprintf(stderr, "[WARNING] stops function must specify a stops array\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - - JSVal value_stops = value["stops"]; - if (!value_stops.IsArray()) { - fprintf(stderr, "[WARNING] stops function must specify a stops array\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - - std::vector> stops; - for (rapidjson::SizeType i = 0; i < value_stops.Size(); ++i) { - JSVal stop = value_stops[i]; - if (stop.IsArray()) { - if (stop.Size() != 2) { - fprintf(stderr, "[WARNING] stop must have zoom level and value specification\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - - JSVal z = stop[rapidjson::SizeType(0)]; - if (!z.IsNumber()) { - fprintf(stderr, "[WARNING] zoom level in stop must be a number\n"); - return std::tuple> { false, ConstantFunction(T()) }; - } - - stops.emplace_back(z.GetDouble(), parseFunctionArgument(stop[rapidjson::SizeType(1)])); - } else { - fprintf(stderr, "[WARNING] function argument must be a numeric value\n"); + std::vector> stops; + for (rapidjson::SizeType i = 0; i < value_stops.Size(); ++i) { + JSVal stop = value_stops[i]; + if (stop.IsArray()) { + if (stop.Size() != 2) { + fprintf(stderr, "[WARNING] stop must have zoom level and value specification\n"); return std::tuple> { false, ConstantFunction(T()) }; } + + JSVal z = stop[rapidjson::SizeType(0)]; + if (!z.IsNumber()) { + fprintf(stderr, "[WARNING] zoom level in stop must be a number\n"); + return std::tuple> { false, ConstantFunction(T()) }; + } + + stops.emplace_back(z.GetDouble(), parseFunctionArgument(stop[rapidjson::SizeType(1)])); + } else { + fprintf(stderr, "[WARNING] function argument must be a numeric value\n"); + return std::tuple> { false, ConstantFunction(T()) }; } - - return std::tuple> { true, StopsFunction(stops) }; - } - else { - fprintf(stderr, "[WARNING] function type '%s' is unknown\n", type.c_str()); } - return std::tuple> { false, ConstantFunction(T()) }; + return std::tuple> { true, StopsFunction(stops, base) }; } diff --git a/test/fixtures/styles/road-width.style.json b/test/fixtures/styles/road-width.style.json index 5738546b3..76d7b5258 100644 --- a/test/fixtures/styles/road-width.style.json +++ b/test/fixtures/styles/road-width.style.json @@ -23,7 +23,6 @@ "type": "line", "style": { "line-width": { - "fn": "stops", "stops": [[13, 0], [13.999, 0], [14, 4], [14.1, 10], [14.2, 20]] } } diff --git a/test/functions.cpp b/test/functions.cpp index a2ad93c4e..56b2a3170 100644 --- a/test/functions.cpp +++ b/test/functions.cpp @@ -19,11 +19,11 @@ TEST(Function, Constant) { TEST(Function, Stops) { // Explicit constant slope in fringe regions. - mbgl::StopsFunction slope_1({ { 0, 1.5 }, { 6, 1.5 }, { 8, 3 }, { 22, 3 } }); + mbgl::StopsFunction slope_1({ { 0, 1.5 }, { 6, 1.5 }, { 8, 3 }, { 22, 3 } }, 1.75); EXPECT_EQ(1.5, slope_1.evaluate(0)); EXPECT_EQ(1.5, slope_1.evaluate(4)); EXPECT_EQ(1.5, slope_1.evaluate(6)); - ASSERT_FLOAT_EQ(2.12132, slope_1.evaluate(7)); + ASSERT_FLOAT_EQ(2.0454545454545454, slope_1.evaluate(7)); EXPECT_EQ(3.0, slope_1.evaluate(8)); EXPECT_EQ(3.0, slope_1.evaluate(9)); EXPECT_EQ(3.0, slope_1.evaluate(15)); @@ -31,48 +31,28 @@ TEST(Function, Stops) { // Test constant values in fringe regions. - mbgl::StopsFunction slope_2({ { 6, 1.5 }, { 8, 3 } }); + mbgl::StopsFunction slope_2({ { 6, 1.5 }, { 8, 3 } }, 1.75); EXPECT_EQ(1.5, slope_2.evaluate(0)); EXPECT_EQ(1.5, slope_2.evaluate(4)); EXPECT_EQ(1.5, slope_2.evaluate(6)); - ASSERT_FLOAT_EQ(2.12132, slope_2.evaluate(7)); + ASSERT_FLOAT_EQ(2.0454545454545454, slope_2.evaluate(7)); EXPECT_EQ(3.0, slope_2.evaluate(8)); EXPECT_EQ(3.0, slope_2.evaluate(9)); EXPECT_EQ(3.0, slope_2.evaluate(15)); EXPECT_EQ(3.0, slope_2.evaluate(22)); // Test no values. - mbgl::StopsFunction slope_3({}); + mbgl::StopsFunction slope_3({}, 1.75); EXPECT_EQ(1, slope_3.evaluate(2)); EXPECT_EQ(1, slope_3.evaluate(6)); EXPECT_EQ(1, slope_3.evaluate(12)); + + + // Explicit constant slope in fringe regions. + mbgl::StopsFunction slope_4({ { 0, 2 }, { 8, 10 } }, 1); + EXPECT_EQ(2, slope_4.evaluate(0)); + EXPECT_EQ(3, slope_4.evaluate(1)); + EXPECT_EQ(4, slope_4.evaluate(2)); + EXPECT_EQ(4.75, slope_4.evaluate(2.75)); + EXPECT_EQ(10, slope_4.evaluate(8)); } - - -TEST(Function, Linear) { - mbgl::LinearFunction slope_1(/* val */ 7.5, /* z_base */ 4, /* slope */ 2, /* min */ 7.5, /* max */ 20); - ASSERT_FLOAT_EQ(7.5, slope_1.evaluate(3)); - ASSERT_FLOAT_EQ(7.5, slope_1.evaluate(4)); - ASSERT_FLOAT_EQ(8.5, slope_1.evaluate(4.5)); - ASSERT_FLOAT_EQ(9.5, slope_1.evaluate(5)); - ASSERT_FLOAT_EQ(11.5, slope_1.evaluate(6)); - ASSERT_FLOAT_EQ(19.5, slope_1.evaluate(10)); - ASSERT_FLOAT_EQ(20, slope_1.evaluate(11)); - ASSERT_FLOAT_EQ(20, slope_1.evaluate(20)); -} - - -TEST(Function, Exponential) { - mbgl::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 - ASSERT_FLOAT_EQ(11, slope_1.evaluate(5)); // 7.5 + 1.75^(5 - 4) * 2 - ASSERT_FLOAT_EQ(13.625, slope_1.evaluate(6)); // 7.5 + 1.75^(6 - 4) * 2 - ASSERT_FLOAT_EQ(18.21875, slope_1.evaluate(7)); // 7.5 + 1.75^(7 - 4) * 2 - ASSERT_FLOAT_EQ(20, slope_1.evaluate(8)); // 7.5 + 1.75^(8 - 4) * 2 ==> clamped to 20 - ASSERT_FLOAT_EQ(20, slope_1.evaluate(20)); // 7.5 + 1.75^(20 - 4) * 2 ==> clamped to 20 -} - - -