Fix edge case in composite function interpolation (#8613)
This fixes a bug where, for a zoom value greater than that of the highest zoom stop, composite function interpolation would return nan. (Blocking a render test over in #8593) * Add failing tests for composite function edge case The failing cases here are: - Should interpolate before the first stop - Should interpolate past the last stop * Fix edge case in composite function interpolation * Hold functions constant outside stop-defined domain
This commit is contained in:
@@ -87,6 +87,7 @@ set(MBGL_TEST_FILES
|
||||
|
||||
# style/function
|
||||
test/style/function/camera_function.test.cpp
|
||||
test/style/function/composite_function.test.cpp
|
||||
test/style/function/source_function.test.cpp
|
||||
|
||||
# style
|
||||
|
||||
@@ -56,9 +56,13 @@ public:
|
||||
assert(!s.stops.empty());
|
||||
auto minIt = s.stops.lower_bound(zoom);
|
||||
auto maxIt = s.stops.upper_bound(zoom);
|
||||
if (minIt != s.stops.begin()) {
|
||||
|
||||
// lower_bound yields first element >= zoom, but we want the *last*
|
||||
// element <= zoom, so if we found a stop > zoom, back up by one.
|
||||
if (minIt != s.stops.begin() && minIt->first > zoom) {
|
||||
minIt--;
|
||||
}
|
||||
|
||||
return std::make_tuple(
|
||||
Range<float> {
|
||||
minIt == s.stops.end() ? s.stops.rbegin()->first : minIt->first,
|
||||
@@ -96,6 +100,9 @@ public:
|
||||
T evaluate(float zoom, const GeometryTileFeature& feature, T finalDefaultValue) const {
|
||||
std::tuple<Range<float>, Range<InnerStops>> ranges = coveringRanges(zoom);
|
||||
Range<T> resultRange = evaluate(std::get<1>(ranges), feature, finalDefaultValue);
|
||||
// If the covering stop range is constant, just return the output value directly.
|
||||
if (resultRange.min == resultRange.max) return resultRange.min;
|
||||
// Otherwise, interpolate between the two stops.
|
||||
return util::interpolate(
|
||||
resultRange.min,
|
||||
resultRange.max,
|
||||
|
||||
46
test/style/function/composite_function.test.cpp
Normal file
46
test/style/function/composite_function.test.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <mbgl/test/util.hpp>
|
||||
#include <mbgl/test/stub_geometry_tile_feature.hpp>
|
||||
|
||||
#include <mbgl/style/function/composite_function.hpp>
|
||||
|
||||
using namespace mbgl;
|
||||
using namespace mbgl::style;
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
static StubGeometryTileFeature oneInteger {
|
||||
PropertyMap {{ "property", uint64_t(1) }}
|
||||
};
|
||||
|
||||
TEST(CompositeFunction, ZoomInterpolation) {
|
||||
EXPECT_EQ(40.0f, CompositeFunction<float>("property", CompositeExponentialStops<float>({
|
||||
{0.0f, {{uint64_t(1), 24.0f}}},
|
||||
{1.5f, {{uint64_t(1), 36.0f}}},
|
||||
{3.0f, {{uint64_t(1), 48.0f}}}
|
||||
}), 0.0f)
|
||||
.evaluate(2.0f, oneInteger, -1.0f)) << "Should interpolate between stops";
|
||||
|
||||
EXPECT_EQ(33.0, CompositeFunction<float>("property", CompositeExponentialStops<float>({
|
||||
{5.0f, {{uint64_t(1), 33.0f}}},
|
||||
{10.0f, {{uint64_t(1), 66.0f}}}
|
||||
}), 0.0f)
|
||||
.evaluate(0.0f, oneInteger, -1.0f)) << "Use first stop output for input values from -inf to first stop";
|
||||
|
||||
EXPECT_EQ(66.0, CompositeFunction<float>("property", CompositeExponentialStops<float>({
|
||||
{0.0f, {{uint64_t(1), 33.0f}}},
|
||||
{10.0f, {{uint64_t(1), 66.0f}}}
|
||||
}), 0.0f)
|
||||
.evaluate(20.0f, oneInteger, -1.0f)) << "Use last stop output for input values from last stop to +inf";
|
||||
|
||||
EXPECT_EQ(66.0f, CompositeFunction<float>("property", CompositeExponentialStops<float>({
|
||||
{0.0f, {{uint64_t(1), 33.0f}}},
|
||||
{10.0f, {{uint64_t(1), 66.0f}}}
|
||||
}), 0.0f)
|
||||
.evaluate(10.0f, oneInteger, -1.0f)) << "Should interpolate TO the last stop.";
|
||||
|
||||
EXPECT_EQ(33.0f, CompositeFunction<float>("property", CompositeExponentialStops<float>({
|
||||
{0.0f, {{uint64_t(1), 33.0f}}},
|
||||
{10.0f, {{uint64_t(1), 66.0f}}}
|
||||
}), 0.0f)
|
||||
.evaluate(0.0f, oneInteger, -1.0f)) << "Should interpolate TO the first stop";
|
||||
}
|
||||
Reference in New Issue
Block a user