diff --git a/bin/style.js b/bin/style.js index 7d2b3557d..8568c282d 100644 --- a/bin/style.js +++ b/bin/style.js @@ -240,6 +240,7 @@ module.exports = { "opacity": [ "linear", 15, 0, 1.0, 0, 0.75 ] }, "restaurant_poi": { + "enabled": [ "min", 18 ], "type": "point", "color": "#cccccc", "size": 18, diff --git a/include/llmr/style/properties.hpp b/include/llmr/style/properties.hpp index 10299d051..47b6ff195 100644 --- a/include/llmr/style/properties.hpp +++ b/include/llmr/style/properties.hpp @@ -34,40 +34,31 @@ enum class Property { namespace functions { float null(float z, const std::vector&); -bool null(float z, const std::vector&); - float constant(float z, const std::vector& values); -bool constant(float z, const std::vector& values); - +float min(float z, const std::vector& values); +float max(float z, const std::vector& values); float stops(float z, const std::vector& values); -bool stops(float z, const std::vector& values); - float linear(float z, const std::vector& values); -bool linear(float z, const std::vector& values); - float exponential(float z, const std::vector& values); -bool exponential(float z, const std::vector& values); } - -template struct FunctionProperty { - typedef T (*fn)(float z, const std::vector& values); + typedef float (*fn)(float z, const std::vector& values); fn function; - std::vector values; + std::vector values; inline FunctionProperty() : function(&functions::null) {} - inline FunctionProperty(T value) : function(&functions::constant), values(1, value) {} - inline T operator()(float z) const { return function(z, values); } + inline FunctionProperty(float value) : function(&functions::constant), values(1, value) {} + template inline T evaluate(float z) const { return function(z, values); } }; struct PointClass { - FunctionProperty enabled = true; - FunctionProperty size; + FunctionProperty enabled = true; + FunctionProperty size; Color color = {{ 0, 0, 0, 1 }}; - FunctionProperty opacity = 1; + FunctionProperty opacity = 1; std::string image; }; @@ -80,11 +71,11 @@ struct PointProperties { }; struct LineClass { - FunctionProperty enabled = true; - FunctionProperty width; - FunctionProperty offset; + FunctionProperty enabled = true; + FunctionProperty width; + FunctionProperty offset; Color color = {{ 0, 0, 0, 1 }}; - FunctionProperty opacity = 1; + FunctionProperty opacity = 1; }; struct LineProperties { @@ -96,12 +87,12 @@ struct LineProperties { }; struct FillClass { - FunctionProperty enabled = true; + FunctionProperty enabled = true; Winding winding = Winding::NonZero; - FunctionProperty antialias = true; + FunctionProperty antialias = true; Color fill_color = {{ 0, 0, 0, 1 }}; Color stroke_color = {{ 0, 0, 0, std::numeric_limits::infinity() }}; - FunctionProperty opacity = 1; + FunctionProperty opacity = 1; std::string image; }; @@ -116,13 +107,13 @@ struct FillProperties { }; struct TextClass { - FunctionProperty enabled = true; + FunctionProperty enabled = true; Color color = {{ 0, 0, 0, 1 }}; Color halo = {{ 1, 1, 1, 0.75 }}; - FunctionProperty haloRadius = 0.25f; - FunctionProperty size = 12.0f; - FunctionProperty rotate = 0.0f; - FunctionProperty alwaysVisible = false; + FunctionProperty haloRadius = 0.25f; + FunctionProperty size = 12.0f; + FunctionProperty rotate = 0.0f; + FunctionProperty alwaysVisible = false; }; struct TextProperties { diff --git a/include/llmr/style/style.hpp b/include/llmr/style/style.hpp index 2323228b0..a004df438 100644 --- a/include/llmr/style/style.hpp +++ b/include/llmr/style/style.hpp @@ -30,16 +30,6 @@ public: void cascade(float z); -private: - static std::pair parseBucket(pbf data); - static LayerDescription parseLayer(pbf data); - static std::pair parseClass(pbf data); - static std::pair parseFillClass(pbf data); - static std::pair parseLineClass(pbf data); - static std::pair parsePointClass(pbf data); - template static FunctionProperty parseProperty(pbf data); - static Color parseColor(pbf& data); - public: std::shared_ptr sprite; diff --git a/include/llmr/style/style_parser.hpp b/include/llmr/style/style_parser.hpp index 93cd373b6..60bb9a050 100644 --- a/include/llmr/style/style_parser.hpp +++ b/include/llmr/style/style_parser.hpp @@ -30,9 +30,8 @@ private: std::string parseString(JSVal value); Color parseColor(JSVal value); Value parseValue(JSVal value); - template typename FunctionProperty::fn parseFunctionType(JSVal type); - FunctionProperty parseFloatFunction(JSVal value); - FunctionProperty parseBoolFunction(JSVal value); + FunctionProperty::fn parseFunctionType(JSVal type); + FunctionProperty parseFunction(JSVal value); private: std::map constants; diff --git a/src/style/properties.cpp b/src/style/properties.cpp index 332f61249..7e2fb6e65 100644 --- a/src/style/properties.cpp +++ b/src/style/properties.cpp @@ -12,20 +12,20 @@ float functions::null(float z, const std::vector&) { return 0; } -bool functions::null(float z, const std::vector&) { - return false; -} - float functions::constant(float z, const std::vector& values) { assert(values.size() == 1); return values.front(); } -bool functions::constant(float z, const std::vector& values) { +float functions::min(float z, const std::vector& values) { assert(values.size() == 1); - return values.front(); + return z >= values.front(); } +float functions::max(float z, const std::vector& values) { + assert(values.size() == 1); + return z <= values.front(); +} float functions::stops(float z, const std::vector& stops) { // We need an even number of stop values. @@ -66,11 +66,6 @@ float functions::stops(float z, const std::vector& stops) { } } -bool functions::stops(float z, const std::vector& values) { - // TODO - return false; -} - float functions::linear(float z, const std::vector& values) { if (values.size() != 5) { return 0; @@ -85,11 +80,6 @@ float functions::linear(float z, const std::vector& values) { return fmin(fmax(min, val + (z - z_base) * slope), max); } -bool functions::linear(float z, const std::vector&) { - // TODO - return false; -} - float functions::exponential(float z, const std::vector& values) { if (values.size() != 5) { return 0; @@ -102,10 +92,4 @@ float functions::exponential(float z, const std::vector& values) { const float max = values[4]; return fmin(fmax(min, val + pow(1.75, (z - z_base)) * slope), max); - // return fmin(fmax(min, val + (z - z_base) * slope), max); -} - -bool functions::exponential(float z, const std::vector&) { - // TODO - return false; } diff --git a/src/style/style.cpp b/src/style/style.cpp index a8a8bfeb2..7e72623f9 100644 --- a/src/style/style.cpp +++ b/src/style/style.cpp @@ -37,12 +37,12 @@ void Style::cascade(float z) { // TODO: This should be restricted to fill styles that have actual // values so as to not override with default values. llmr::FillProperties& fill = computed.fills[layer_name]; - fill.enabled = layer.enabled(z); + fill.enabled = layer.enabled.evaluate(z); fill.winding = layer.winding; - fill.antialias = layer.antialias(z); + fill.antialias = layer.antialias.evaluate(z); fill.fill_color = layer.fill_color; fill.stroke_color = layer.stroke_color; - fill.opacity = layer.opacity(z); + fill.opacity = layer.opacity.evaluate(z); fill.image = layer.image; } @@ -54,11 +54,11 @@ void Style::cascade(float z) { // TODO: This should be restricted to line styles that have actual // values so as to not override with default values. llmr::LineProperties& stroke = computed.lines[layer_name]; - stroke.enabled = layer.enabled(z); - stroke.width = layer.width(z); - stroke.offset = layer.offset(z); + stroke.enabled = layer.enabled.evaluate(z); + stroke.width = layer.width.evaluate(z); + stroke.offset = layer.offset.evaluate(z); stroke.color = layer.color; - stroke.opacity = layer.opacity(z); + stroke.opacity = layer.opacity.evaluate(z); } // Cascade point classes @@ -69,10 +69,10 @@ void Style::cascade(float z) { // TODO: This should be restricted to point styles that have actual // values so as to not override with default values. llmr::PointProperties& point = computed.points[layer_name]; - point.enabled = layer.enabled(z); + point.enabled = layer.enabled.evaluate(z); point.color = layer.color; - point.size = layer.size(z); - point.opacity = layer.opacity(z); + point.size = layer.size.evaluate(z); + point.opacity = layer.opacity.evaluate(z); point.image = layer.image; } @@ -83,13 +83,13 @@ void Style::cascade(float z) { // TODO: This should be restricted to point styles that have actual // values so as to not override with default values. llmr::TextProperties& text = computed.texts[layer_name]; - text.enabled = layer.enabled(z); + text.enabled = layer.enabled.evaluate(z); text.color = layer.color; - text.size = layer.size(z); + text.size = layer.size.evaluate(z); text.halo = layer.halo; - text.haloRadius = layer.haloRadius(z); - text.rotate = layer.rotate(z); - text.alwaysVisible = layer.alwaysVisible(z); + text.haloRadius = layer.haloRadius.evaluate(z); + text.rotate = layer.rotate.evaluate(z); + text.alwaysVisible = layer.alwaysVisible.evaluate(z); } // Cascade background diff --git a/src/style/style_parser.cpp b/src/style/style_parser.cpp index 1902af2dc..7c452f94d 100644 --- a/src/style/style_parser.cpp +++ b/src/style/style_parser.cpp @@ -319,8 +319,7 @@ Color StyleParser::parseColor(JSVal value) { } } -template -typename FunctionProperty::fn StyleParser::parseFunctionType(JSVal type) { +FunctionProperty::fn StyleParser::parseFunctionType(JSVal type) { if (type.IsString()) { std::string t { type.GetString(), type.GetStringLength() }; if (t == "constant") { @@ -331,6 +330,10 @@ typename FunctionProperty::fn StyleParser::parseFunctionType(JSVal type) { return &functions::stops; } else if (t == "exponential") { return &functions::exponential; + } else if (t == "min") { + return &functions::min; + } else if (t == "max") { + return &functions::max; } else { throw Style::exception("unknown function type"); } @@ -339,15 +342,15 @@ typename FunctionProperty::fn StyleParser::parseFunctionType(JSVal type) { } } -FunctionProperty StyleParser::parseFloatFunction(JSVal value) { - FunctionProperty property; +FunctionProperty StyleParser::parseFunction(JSVal value) { + FunctionProperty property; if (value.IsArray()) { if (value.Size() < 1) { throw Style::exception("value function does not have arguments"); } - property.function = parseFunctionType(value[(rapidjson::SizeType)0]); + property.function = parseFunctionType(value[(rapidjson::SizeType)0]); for (rapidjson::SizeType i = 1; i < value.Size(); ++i) { JSVal stop = value[i]; if (stop.IsObject()) { @@ -370,8 +373,10 @@ FunctionProperty StyleParser::parseFloatFunction(JSVal value) { property.values.push_back(val.GetDouble()); } else if (stop.IsNumber()) { property.values.push_back(stop.GetDouble()); + } else if (stop.IsBool()) { + property.values.push_back(stop.GetBool()); } else { - throw Style::exception("stop must be a number"); + throw Style::exception("function argument must be a numeric value"); } } } else if (value.IsNumber()) { @@ -382,54 +387,11 @@ FunctionProperty StyleParser::parseFloatFunction(JSVal value) { return property; } -FunctionProperty StyleParser::parseBoolFunction(JSVal value) { - FunctionProperty property; - - if (value.IsArray()) { - if (value.Size() < 1) { - throw Style::exception("value function does not have arguments"); - } - - property.function = parseFunctionType(value[(rapidjson::SizeType)0]); - for (rapidjson::SizeType i = 1; i < value.Size(); ++i) { - JSVal stop = value[i]; - if (stop.IsObject()) { - if (!stop.HasMember("z")) { - throw Style::exception("stop must have zoom level specification"); - } - JSVal z = stop["z"]; - if (!z.IsBool()) { - throw Style::exception("zoom level in stops must be a number"); - } - property.values.push_back(z.GetBool()); - - if (!stop.HasMember("val")) { - throw Style::exception("stop must have value specification"); - } - JSVal val = stop["val"]; - if (!val.IsBool()) { - throw Style::exception("value in stops must be a number"); - } - property.values.push_back(val.GetBool()); - } else if (stop.IsBool()) { - property.values.push_back(stop.GetBool()); - } else { - throw Style::exception("stop must be a number"); - } - } - } else if (value.IsBool()) { - property.function = &functions::constant; - property.values.push_back(value.GetBool()); - } - - return property; -} - FillClass StyleParser::parseFillClass(JSVal value) { FillClass klass; if (value.HasMember("enabled")) { - klass.enabled = parseBoolFunction(value["enabled"]); + klass.enabled = parseFunction(value["enabled"]); } if (value.HasMember("color")) { @@ -451,7 +413,7 @@ FillClass StyleParser::parseFillClass(JSVal value) { } if (value.HasMember("opacity")) { - klass.opacity = parseFloatFunction(value["opacity"]); + klass.opacity = parseFunction(value["opacity"]); } return klass; @@ -461,7 +423,7 @@ LineClass StyleParser::parseLineClass(JSVal value) { LineClass klass; if (value.HasMember("enabled")) { - klass.enabled = parseBoolFunction(value["enabled"]); + klass.enabled = parseFunction(value["enabled"]); } if (value.HasMember("color")) { @@ -469,11 +431,11 @@ LineClass StyleParser::parseLineClass(JSVal value) { } if (value.HasMember("width")) { - klass.width = parseFloatFunction(value["width"]); + klass.width = parseFunction(value["width"]); } if (value.HasMember("opacity")) { - klass.opacity = parseFloatFunction(value["opacity"]); + klass.opacity = parseFunction(value["opacity"]); } return klass; @@ -483,7 +445,7 @@ PointClass StyleParser::parsePointClass(JSVal value) { PointClass klass; if (value.HasMember("enabled")) { - klass.enabled = parseBoolFunction(value["enabled"]); + klass.enabled = parseFunction(value["enabled"]); } if (value.HasMember("color")) { @@ -491,7 +453,7 @@ PointClass StyleParser::parsePointClass(JSVal value) { } if (value.HasMember("opacity")) { - klass.opacity = parseFloatFunction(value["opacity"]); + klass.opacity = parseFunction(value["opacity"]); } if (value.HasMember("image")) { @@ -499,7 +461,7 @@ PointClass StyleParser::parsePointClass(JSVal value) { } if (value.HasMember("size")) { - klass.size = parseFloatFunction(value["size"]); + klass.size = parseFunction(value["size"]); } return klass; @@ -510,7 +472,7 @@ TextClass StyleParser::parseTextClass(JSVal value) { TextClass klass; if (value.HasMember("enabled")) { - klass.enabled = parseBoolFunction(value["enabled"]); + klass.enabled = parseFunction(value["enabled"]); } if (value.HasMember("color")) { @@ -522,19 +484,19 @@ TextClass StyleParser::parseTextClass(JSVal value) { } if (value.HasMember("strokeWidth")) { - klass.haloRadius = parseFloatFunction(value["strokeWidth"]); + klass.haloRadius = parseFunction(value["strokeWidth"]); } if (value.HasMember("size")) { - klass.size = parseFloatFunction(value["size"]); + klass.size = parseFunction(value["size"]); } if (value.HasMember("rotate")) { - klass.rotate = parseFloatFunction(value["rotate"]); + klass.rotate = parseFunction(value["rotate"]); } if (value.HasMember("alwaysVisible")) { - klass.alwaysVisible = parseBoolFunction(value["alwaysVisible"]); + klass.alwaysVisible = parseFunction(value["alwaysVisible"]); } return klass;