From 531a1c3070a098b5b31082c0500f0a3ce6a500bb Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 17 Jan 2017 11:48:37 -0800 Subject: [core] Fix calculation of delayed transitions --- cmake/test-files.cmake | 2 + src/mbgl/style/paint_property.hpp | 3 ++ test/style/paint_property.test.cpp | 94 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 test/style/paint_property.test.cpp diff --git a/cmake/test-files.cmake b/cmake/test-files.cmake index 7eca71299f..bf908e758c 100644 --- a/cmake/test-files.cmake +++ b/cmake/test-files.cmake @@ -55,6 +55,7 @@ set(MBGL_TEST_FILES test/src/mbgl/test/stub_file_source.hpp test/src/mbgl/test/stub_layer_observer.hpp test/src/mbgl/test/stub_style_observer.hpp + test/src/mbgl/test/stub_tile_observer.hpp test/src/mbgl/test/test.cpp test/src/mbgl/test/util.cpp test/src/mbgl/test/util.hpp @@ -80,6 +81,7 @@ set(MBGL_TEST_FILES test/style/filter.test.cpp test/style/functions.test.cpp test/style/group_by_layout.test.cpp + test/style/paint_property.test.cpp test/style/source.test.cpp test/style/style.test.cpp test/style/style_layer.test.cpp diff --git a/src/mbgl/style/paint_property.hpp b/src/mbgl/style/paint_property.hpp index 15df2a77c7..7b56f6415d 100644 --- a/src/mbgl/style/paint_property.hpp +++ b/src/mbgl/style/paint_property.hpp @@ -45,6 +45,9 @@ public: // Transition from prior value is now complete. prior = {}; return finalValue; + } else if (parameters.now < begin) { + // Transition hasn't started yet. + return prior->get().evaluate(parameters, defaultValue); } else { // Interpolate between recursively-calculated prior value and final. float t = std::chrono::duration(parameters.now - begin) / (end - begin); diff --git a/test/style/paint_property.test.cpp b/test/style/paint_property.test.cpp new file mode 100644 index 0000000000..487dbe9652 --- /dev/null +++ b/test/style/paint_property.test.cpp @@ -0,0 +1,94 @@ +#include + +#include + +using namespace mbgl; +using namespace mbgl::style; +using namespace std::literals::chrono_literals; + +TEST(UnevaluatedPaintProperty, EvaluateDefaultValue) { + UnevaluatedPaintProperty> property; + ASSERT_EQ(0.0f, property.evaluate(PropertyEvaluationParameters(0), 0.0f)); +} + +TEST(UnevaluatedPaintProperty, EvaluateUntransitionedConstant) { + UnevaluatedPaintProperty> property { + PropertyValue(1.0f), + UnevaluatedPaintProperty>(), + TransitionOptions(), + TimePoint::min() + }; + + ASSERT_EQ(1.0f, property.evaluate(PropertyEvaluationParameters(0), 0.0f)); +} + +TEST(UnevaluatedPaintProperty, EvaluateTransitionedConstantWithoutDelay) { + TransitionOptions transition; + transition.duration = { 1000ms }; + + UnevaluatedPaintProperty> t0 { + PropertyValue(0.0f), + UnevaluatedPaintProperty>(), + TransitionOptions(), + TimePoint::min() + }; + + UnevaluatedPaintProperty> t1 { + PropertyValue(1.0f), + t0, + transition, + TimePoint::min() + }; + + auto evaluate = [&] (Duration delta) { + PropertyEvaluationParameters parameters { + 0, + TimePoint::min() + delta, + ZoomHistory(), + Duration::zero() + }; + + return t1.evaluate(parameters, 0.0f); + }; + + ASSERT_FLOAT_EQ(0.0f, evaluate(0ms)); + ASSERT_FLOAT_EQ(0.823099f, evaluate(500ms)); + ASSERT_FLOAT_EQ(1.0f, evaluate(1500ms)); +} + +TEST(UnevaluatedPaintProperty, EvaluateTransitionedConstantWithDelay) { + TransitionOptions transition; + transition.delay = { 1000ms }; + transition.duration = { 1000ms }; + + UnevaluatedPaintProperty> t0 { + PropertyValue(0.0f), + UnevaluatedPaintProperty>(), + TransitionOptions(), + TimePoint::min() + }; + + UnevaluatedPaintProperty> t1 { + PropertyValue(1.0f), + t0, + transition, + TimePoint::min() + }; + + auto evaluate = [&] (Duration delta) { + PropertyEvaluationParameters parameters { + 0, + TimePoint::min() + delta, + ZoomHistory(), + Duration::zero() + }; + + return t1.evaluate(parameters, 0.0f); + }; + + ASSERT_FLOAT_EQ(0.0f, evaluate(0ms)); + ASSERT_FLOAT_EQ(0.0f, evaluate(500ms)); + ASSERT_FLOAT_EQ(0.0f, evaluate(612ms)); + ASSERT_FLOAT_EQ(0.823099f, evaluate(1500ms)); + ASSERT_FLOAT_EQ(1.0f, evaluate(2500ms)); +} -- cgit v1.2.1