diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-03 15:04:25 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2015-11-06 10:38:48 -0800 |
commit | b81243daa9ec5a16594fa280236e2ee903540f64 (patch) | |
tree | 024376bc2a72890d6c77eb1581d2e716f661ea87 /src | |
parent | 7de4ce5bcbd8a3081f364bef3249d99f3fad60cc (diff) | |
download | qtlocation-mapboxgl-b81243daa9ec5a16594fa280236e2ee903540f64.tar.gz |
[core] Merge PiecewiseConstantFunction into Function
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/layer/background_layer.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/layer/fill_layer.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/layer/line_layer.cpp | 4 | ||||
-rw-r--r-- | src/mbgl/style/class_properties.hpp | 3 | ||||
-rw-r--r-- | src/mbgl/style/function.cpp | 82 | ||||
-rw-r--r-- | src/mbgl/style/function.hpp | 31 | ||||
-rw-r--r-- | src/mbgl/style/piecewisefunction_properties.cpp | 54 | ||||
-rw-r--r-- | src/mbgl/style/piecewisefunction_properties.hpp | 35 | ||||
-rw-r--r-- | src/mbgl/style/property_evaluator.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/style/property_parsing.cpp | 90 | ||||
-rw-r--r-- | src/mbgl/style/property_value.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/style/style_calculation_parameters.hpp | 2 |
12 files changed, 135 insertions, 180 deletions
diff --git a/src/mbgl/layer/background_layer.cpp b/src/mbgl/layer/background_layer.cpp index bbfd56a1b3..d52905708b 100644 --- a/src/mbgl/layer/background_layer.cpp +++ b/src/mbgl/layer/background_layer.cpp @@ -7,7 +7,7 @@ void BackgroundLayer::parsePaints(const JSVal& layer) { paints.parseEach(layer, [&] (ClassProperties& paint, const JSVal& value) { parseProperty<Function<float>>("background-opacity", PropertyKey::BackgroundOpacity, paint, value); parseProperty<Function<Color>>("background-color", PropertyKey::BackgroundColor, paint, value); - parseProperty<PiecewiseConstantFunction<Faded<std::string>>>("background-pattern", PropertyKey::BackgroundImage, paint, value, "background-pattern-transition"); + parseProperty<Function<Faded<std::string>>>("background-pattern", PropertyKey::BackgroundImage, paint, value, "background-pattern-transition"); }); } diff --git a/src/mbgl/layer/fill_layer.cpp b/src/mbgl/layer/fill_layer.cpp index 5ff26337b7..7d53fa97cb 100644 --- a/src/mbgl/layer/fill_layer.cpp +++ b/src/mbgl/layer/fill_layer.cpp @@ -17,7 +17,7 @@ void FillLayer::parsePaints(const JSVal& layer) { parseProperty<Function<std::array<float, 2>>>("fill-translate", PropertyKey::FillTranslate, paint, value); parseProperty<PropertyTransition>("fill-translate-transition", PropertyKey::FillTranslate, paint, value); parseProperty<Function<TranslateAnchorType>>("fill-translate-anchor", PropertyKey::FillTranslateAnchor, paint, value); - parseProperty<PiecewiseConstantFunction<Faded<std::string>>>("fill-pattern", PropertyKey::FillImage, paint, value, "fill-pattern-transition"); + parseProperty<Function<Faded<std::string>>>("fill-pattern", PropertyKey::FillImage, paint, value, "fill-pattern-transition"); }); } diff --git a/src/mbgl/layer/line_layer.cpp b/src/mbgl/layer/line_layer.cpp index a27cf65ee2..a0b216bda1 100644 --- a/src/mbgl/layer/line_layer.cpp +++ b/src/mbgl/layer/line_layer.cpp @@ -28,8 +28,8 @@ void LineLayer::parsePaints(const JSVal& layer) { parseProperty<PropertyTransition>("line-gap-width-transition", PropertyKey::LineGapWidth, paint, value); parseProperty<Function<float>>("line-blur", PropertyKey::LineBlur, paint, value); parseProperty<PropertyTransition>("line-blur-transition", PropertyKey::LineBlur, paint, value); - parseProperty<PiecewiseConstantFunction<Faded<std::vector<float>>>>("line-dasharray", PropertyKey::LineDashArray, paint, value, "line-dasharray-transition"); - parseProperty<PiecewiseConstantFunction<Faded<std::string>>>("line-pattern", PropertyKey::LineImage, paint, value, "line-pattern-transition"); + parseProperty<Function<Faded<std::vector<float>>>>("line-dasharray", PropertyKey::LineDashArray, paint, value, "line-dasharray-transition"); + parseProperty<Function<Faded<std::string>>>("line-pattern", PropertyKey::LineImage, paint, value, "line-pattern-transition"); }); } diff --git a/src/mbgl/style/class_properties.hpp b/src/mbgl/style/class_properties.hpp index 84b91d769e..153fc11ee7 100644 --- a/src/mbgl/style/class_properties.hpp +++ b/src/mbgl/style/class_properties.hpp @@ -34,8 +34,7 @@ public: void calculate(PropertyKey key, T& target, const float z) const { auto it = properties.find(key); if (it != properties.end()) { - const PropertyEvaluator<T> evaluator(z); - target = mapbox::util::apply_visitor(evaluator, it->second); + target = mapbox::util::apply_visitor(PropertyEvaluator<T>(StyleCalculationParameters(z)), it->second); } } diff --git a/src/mbgl/style/function.cpp b/src/mbgl/style/function.cpp index 19ccd7f60c..5c8f294da4 100644 --- a/src/mbgl/style/function.cpp +++ b/src/mbgl/style/function.cpp @@ -1,5 +1,5 @@ #include <mbgl/style/function.hpp> -#include <mbgl/style/types.hpp> +#include <mbgl/style/style_calculation_parameters.hpp> #include <mbgl/util/interpolate.hpp> #include <cmath> @@ -27,7 +27,8 @@ template <> inline TextTransformType defaultStopsValue() { return {}; }; template <> inline RotationAlignmentType defaultStopsValue() { return {}; }; template <typename T> -T Function<T>::evaluate(float z) const { +T Function<T>::evaluate(const StyleCalculationParameters& parameters) const { + float z = parameters.z; bool smaller = false; float smaller_z = 0.0f; T smaller_val = T(); @@ -73,21 +74,66 @@ T Function<T>::evaluate(float z) const { } } -template bool Function<bool>::evaluate(float z) const; -template float Function<float>::evaluate(float z) const; -template Color Function<Color>::evaluate(float z) const; -template std::vector<float> Function<std::vector<float>>::evaluate(float z) const; -template std::array<float, 2> Function<std::array<float, 2>>::evaluate(float z) const; - -template std::string Function<std::string>::evaluate(float z) const; -template TranslateAnchorType Function<TranslateAnchorType>::evaluate(float z) const; -template RotateAnchorType Function<RotateAnchorType>::evaluate(float z) const; -template CapType Function<CapType>::evaluate(float z) const; -template JoinType Function<JoinType>::evaluate(float z) const; -template PlacementType Function<PlacementType>::evaluate(float z) const; -template TextAnchorType Function<TextAnchorType>::evaluate(float z) const; -template TextJustifyType Function<TextJustifyType>::evaluate(float z) const; -template TextTransformType Function<TextTransformType>::evaluate(float z) const; -template RotationAlignmentType Function<RotationAlignmentType>::evaluate(float z) const; +template class Function<bool>; +template class Function<float>; +template class Function<Color>; +template class Function<std::vector<float>>; +template class Function<std::array<float, 2>>; + +template class Function<std::string>; +template class Function<TranslateAnchorType>; +template class Function<RotateAnchorType>; +template class Function<CapType>; +template class Function<JoinType>; +template class Function<PlacementType>; +template class Function<TextAnchorType>; +template class Function<TextJustifyType>; +template class Function<TextTransformType>; +template class Function<RotationAlignmentType>; + +template <typename T> +size_t getBiggestStopLessThan(std::vector<std::pair<float, T>> stops, float z) { + for (uint32_t i = 0; i < stops.size(); i++) { + if (stops[i].first > z) { + return i == 0 ? i : i - 1; + } + } + return stops.size() - 1; +} + +template <typename T> +Faded<T> Function<Faded<T>>::evaluate(const StyleCalculationParameters& parameters) const { + Faded<T> result; + + float z = parameters.z; + float fraction = std::fmod(z, 1.0f); + std::chrono::duration<float> d = duration ? *duration : parameters.defaultFadeDuration; + float t = std::min((Clock::now() - parameters.zoomHistory.lastIntegerZoomTime) / d, 1.0f); + float fromScale = 1.0f; + float toScale = 1.0f; + size_t from, to; + + if (z > parameters.zoomHistory.lastIntegerZoom) { + result.t = fraction + (1.0f - fraction) * t; + from = getBiggestStopLessThan(stops, z - 1.0f); + to = getBiggestStopLessThan(stops, z); + fromScale *= 2.0f; + + } else { + result.t = 1 - (1 - t) * fraction; + to = getBiggestStopLessThan(stops, z); + from = getBiggestStopLessThan(stops, z + 1.0f); + fromScale /= 2.0f; + } + + result.from = stops[from].second; + result.to = stops[to].second; + result.fromScale = fromScale; + result.toScale = toScale; + return result; +} + +template class Function<Faded<std::string>>; +template class Function<Faded<std::vector<float>>>; } diff --git a/src/mbgl/style/function.hpp b/src/mbgl/style/function.hpp index 6ef4fcf5da..e28b0efdd9 100644 --- a/src/mbgl/style/function.hpp +++ b/src/mbgl/style/function.hpp @@ -1,11 +1,18 @@ #ifndef MBGL_STYLE_FUNCTION #define MBGL_STYLE_FUNCTION +#include <mbgl/style/types.hpp> +#include <mbgl/util/chrono.hpp> + +#include <mapbox/optional.hpp> + #include <vector> #include <utility> namespace mbgl { +class StyleCalculationParameters; + template <typename T> class Function { public: @@ -19,13 +26,35 @@ public: explicit Function(const Stops& stops_, float base_) : base(base_), stops(stops_) {} - T evaluate(float z) const; + T evaluate(const StyleCalculationParameters&) const; private: const float base = 1; const std::vector<std::pair<float, T>> stops; }; +// Partial specialization for cross-faded properties (*-pattern, line-dasharray). +template <typename T> +class Function<Faded<T>> { +public: + using Stop = std::pair<float, T>; + using Stops = std::vector<Stop>; + + // TODO: make explicit + /* explicit */ Function(const T& constant) + : stops({{ 0, constant }}) {} + + explicit Function(const Stops& stops_, + mapbox::util::optional<Duration> duration_) + : stops(stops_), duration(duration_) {} + + Faded<T> evaluate(const StyleCalculationParameters&) const; + +private: + const std::vector<std::pair<float, T>> stops; + const mapbox::util::optional<Duration> duration = {}; +}; + } #endif diff --git a/src/mbgl/style/piecewisefunction_properties.cpp b/src/mbgl/style/piecewisefunction_properties.cpp deleted file mode 100644 index d741698299..0000000000 --- a/src/mbgl/style/piecewisefunction_properties.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include <mbgl/style/piecewisefunction_properties.hpp> -#include <mbgl/style/types.hpp> - -#include <cmath> - -namespace mbgl { - -template <typename T> -size_t getBiggestStopLessThan(std::vector<std::pair<float, T>> stops, float z) { - for (uint32_t i = 0; i < stops.size(); i++) { - if (stops[i].first > z) { - return i == 0 ? i : i - 1; - } - } - return stops.size() - 1; -} - -template <typename T> -T PiecewiseConstantFunction<T>::evaluate(const StyleCalculationParameters& parameters) const { - T result; - - float z = parameters.z; - float fraction = std::fmod(z, 1.0f); - std::chrono::duration<float> d = duration ? *duration : parameters.defaultFadeDuration; - float t = std::min((Clock::now() - parameters.zoomHistory.lastIntegerZoomTime) / d, 1.0f); - float fromScale = 1.0f; - float toScale = 1.0f; - size_t from, to; - - if (z > parameters.zoomHistory.lastIntegerZoom) { - result.t = fraction + (1.0f - fraction) * t; - from = getBiggestStopLessThan(values, z - 1.0f); - to = getBiggestStopLessThan(values, z); - fromScale *= 2.0f; - - } else { - result.t = 1 - (1 - t) * fraction; - to = getBiggestStopLessThan(values, z); - from = getBiggestStopLessThan(values, z + 1.0f); - fromScale /= 2.0f; - } - - - result.from = values[from].second.to; - result.to = values[to].second.to; - result.fromScale = fromScale; - result.toScale = toScale; - return result; -} - -template Faded<std::string> PiecewiseConstantFunction<Faded<std::string>>::evaluate(const StyleCalculationParameters&) const; -template Faded<std::vector<float>> PiecewiseConstantFunction<Faded<std::vector<float>>>::evaluate(const StyleCalculationParameters&) const; - -} diff --git a/src/mbgl/style/piecewisefunction_properties.hpp b/src/mbgl/style/piecewisefunction_properties.hpp deleted file mode 100644 index 491442958d..0000000000 --- a/src/mbgl/style/piecewisefunction_properties.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef MBGL_STYLE_FADEDFUNCTION_PROPERTIES -#define MBGL_STYLE_FADEDFUNCTION_PROPERTIES - -#include <mbgl/style/style_calculation_parameters.hpp> -#include <mbgl/util/chrono.hpp> - -#include <mapbox/optional.hpp> - -#include <vector> - -namespace mbgl { - -template <typename T> -struct PiecewiseConstantFunction { - PiecewiseConstantFunction(const std::vector<std::pair<float, T>>& values_, - mapbox::util::optional<Duration> duration_) - : values(values_), - duration(duration_) { - } - - PiecewiseConstantFunction(const T& value) - : values({{ 0, value }}), - duration() { - } - - T evaluate(const StyleCalculationParameters&) const; - -private: - const std::vector<std::pair<float, T>> values; - const mapbox::util::optional<Duration> duration; -}; - -} - -#endif diff --git a/src/mbgl/style/property_evaluator.hpp b/src/mbgl/style/property_evaluator.hpp index dd4960c8ea..81d2c93342 100644 --- a/src/mbgl/style/property_evaluator.hpp +++ b/src/mbgl/style/property_evaluator.hpp @@ -3,7 +3,6 @@ #include <mbgl/style/zoom_history.hpp> #include <mbgl/style/function.hpp> -#include <mbgl/style/piecewisefunction_properties.hpp> #include <mbgl/style/style_calculation_parameters.hpp> namespace mbgl { @@ -24,10 +23,6 @@ public: } T operator()(const Function<T>& value) const { - return value.evaluate(parameters.z); - } - - T operator()(const PiecewiseConstantFunction<T>& value) const { return value.evaluate(parameters); } diff --git a/src/mbgl/style/property_parsing.cpp b/src/mbgl/style/property_parsing.cpp index 6df3d4c0fb..73345fcc62 100644 --- a/src/mbgl/style/property_parsing.cpp +++ b/src/mbgl/style/property_parsing.cpp @@ -7,29 +7,6 @@ namespace mbgl { namespace detail { -optional<std::vector<float>> parseFloatArray(const JSVal& value) { - if (!value.IsArray()) { - Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); - return {}; - } - - std::vector<float> result; - - for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { - const JSVal& part = value[i]; - - if (!part.IsNumber()) { - Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); - return {}; - } - - result.push_back(part.GetDouble()); - } - - return result; -} - - template <> optional<bool> parseProperty(const char* name, const JSVal& value) { if (!value.IsBool()) { @@ -206,6 +183,29 @@ optional<std::array<float, 2>> parseProperty(const char* name, const JSVal& valu } template <> +optional<std::vector<float>> parseProperty(const char* name, const JSVal& value) { + if (!value.IsArray()) { + Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", name); + return {}; + } + + std::vector<float> result; + + for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { + const JSVal& part = value[i]; + + if (!part.IsNumber()) { + Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers", name); + return {}; + } + + result.push_back(part.GetDouble()); + } + + return result; +} + +template <> optional<PropertyTransition> parseProperty(const char *, const JSVal& value) { PropertyTransition transition; if (value.IsObject()) { @@ -360,7 +360,7 @@ template<> optional<Function<Color>> parseProperty(const char* name, const JSVal } template <typename T> -optional<PiecewiseConstantFunction<T>> parsePiecewiseConstantFunction(const JSVal& value, const JSVal& transition) { +optional<Function<Faded<T>>> parseFadedFunction(const JSVal& value, const JSVal& transition) { mapbox::util::optional<Duration> duration; if (transition.HasMember("duration")) { duration = std::chrono::milliseconds(transition["duration"].GetUint()); @@ -377,57 +377,33 @@ optional<PiecewiseConstantFunction<T>> parsePiecewiseConstantFunction(const JSVa return {}; } - return PiecewiseConstantFunction<T>(*stops, duration); -} - -template <> -optional<Faded<std::vector<float>>> parseProperty(const char*, const JSVal& value) { - auto floatarray = parseFloatArray(value); - if (!floatarray) { - return {}; - } - - Faded<std::vector<float>> parsed; - parsed.to = *floatarray; - return parsed; -} - -template <> -optional<Faded<std::string>> parseProperty(const char* name, const JSVal& value) { - if (!value.IsString()) { - Log::Warning(Event::ParseStyle, "value of '%s' must be a string, or a string function", name); - return {}; - } - - Faded<std::string> parsed; - parsed.to = { value.GetString(), value.GetStringLength() }; - return parsed; + return Function<Faded<T>>(*stops, duration); } template <> -optional<PiecewiseConstantFunction<Faded<std::vector<float>>>> parseProperty(const char* name, const JSVal& value, const JSVal& transition) { +optional<Function<Faded<std::vector<float>>>> parseProperty(const char* name, const JSVal& value, const JSVal& transition) { if (value.IsObject()) { - return parsePiecewiseConstantFunction<Faded<std::vector<float>>>(value, transition); + return parseFadedFunction<std::vector<float>>(value, transition); } - auto constant = parseProperty<Faded<std::vector<float>>>(name, value); + auto constant = parseProperty<std::vector<float>>(name, value); if (!constant) { return {}; } - return PiecewiseConstantFunction<Faded<std::vector<float>>>(*constant); + return Function<Faded<std::vector<float>>>(*constant); } template <> -optional<PiecewiseConstantFunction<Faded<std::string>>> parseProperty(const char* name, const JSVal& value, const JSVal& transition) { +optional<Function<Faded<std::string>>> parseProperty(const char* name, const JSVal& value, const JSVal& transition) { if (value.IsObject()) { - return parsePiecewiseConstantFunction<Faded<std::string>>(value, transition); + return parseFadedFunction<std::string>(value, transition); } - auto constant = parseProperty<Faded<std::string>>(name, value); + auto constant = parseProperty<std::string>(name, value); if (!constant) { return {}; } - return PiecewiseConstantFunction<Faded<std::string>>(*constant); + return Function<Faded<std::string>>(*constant); } } diff --git a/src/mbgl/style/property_value.hpp b/src/mbgl/style/property_value.hpp index 60ddb7fc7a..8d6b95753c 100644 --- a/src/mbgl/style/property_value.hpp +++ b/src/mbgl/style/property_value.hpp @@ -4,7 +4,6 @@ #include <mapbox/variant.hpp> #include <mbgl/style/function.hpp> -#include <mbgl/style/piecewisefunction_properties.hpp> #include <mbgl/style/types.hpp> #include <vector> @@ -28,8 +27,8 @@ typedef mapbox::util::variant< Function<bool>, Function<float>, Function<Color>, - PiecewiseConstantFunction<Faded<std::vector<float>>>, - PiecewiseConstantFunction<Faded<std::string>> + Function<Faded<std::vector<float>>>, + Function<Faded<std::string>> > PropertyValue; } diff --git a/src/mbgl/style/style_calculation_parameters.hpp b/src/mbgl/style/style_calculation_parameters.hpp index 3b54fd4bdc..0c42d8fbfc 100644 --- a/src/mbgl/style/style_calculation_parameters.hpp +++ b/src/mbgl/style/style_calculation_parameters.hpp @@ -8,7 +8,7 @@ namespace mbgl { class StyleCalculationParameters { public: - StyleCalculationParameters(float z_) + explicit StyleCalculationParameters(float z_) : z(z_) {} StyleCalculationParameters(float z_, |