Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Replace {Source,Camera,Composite}Function with PropertyExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 19, 2018
1 parent 5d7e3e2 commit 2e55305
Show file tree
Hide file tree
Showing 53 changed files with 702 additions and 951 deletions.
8 changes: 4 additions & 4 deletions benchmark/function/camera_function.benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <benchmark/benchmark.h>

#include <mbgl/style/function/source_function.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion/function.hpp>
#include <mbgl/style/conversion/property_value.hpp>

using namespace mbgl;
using namespace mbgl::style;
Expand All @@ -27,7 +27,7 @@ static void Parse_CameraFunction(benchmark::State& state) {
state.PauseTiming();
auto doc = createFunctionJSON(stopCount);
state.ResumeTiming();
optional<CameraFunction<float>> result = conversion::convertJSON<CameraFunction<float>>(doc, error);
optional<PropertyValue<float>> result = conversion::convertJSON<PropertyValue<float>>(doc, error);
if (!result) {
state.SkipWithError(error.message.c_str());
}
Expand All @@ -39,14 +39,14 @@ static void Evaluate_CameraFunction(benchmark::State& state) {
size_t stopCount = state.range(0);
auto doc = createFunctionJSON(stopCount);
conversion::Error error;
optional<CameraFunction<float>> function = conversion::convertJSON<CameraFunction<float>>(doc, error);
optional<PropertyValue<float>> function = conversion::convertJSON<PropertyValue<float>>(doc, error);
if (!function) {
state.SkipWithError(error.message.c_str());
}

while(state.KeepRunning()) {
float z = 24.0f * static_cast<float>(rand() % 100) / 100;
function->evaluate(z);
function->asExpression().evaluate(z);
}

state.SetLabel(std::to_string(stopCount).c_str());
Expand Down
9 changes: 4 additions & 5 deletions benchmark/function/composite_function.benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

#include <mbgl/benchmark/stub_geometry_tile_feature.hpp>

#include <mbgl/style/function/composite_function.hpp>

#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion/function.hpp>
#include <mbgl/style/conversion/data_driven_property_value.hpp>

using namespace mbgl;
using namespace mbgl::style;
Expand Down Expand Up @@ -34,7 +33,7 @@ static void Parse_CompositeFunction(benchmark::State& state) {
state.PauseTiming();
auto doc = createFunctionJSON(stopCount);
state.ResumeTiming();
optional<CompositeFunction<float>> result = conversion::convertJSON<style::CompositeFunction<float>>(doc, error);
optional<DataDrivenPropertyValue<float>> result = conversion::convertJSON<DataDrivenPropertyValue<float>>(doc, error);
if (!result) {
state.SkipWithError(error.message.c_str());
}
Expand All @@ -46,14 +45,14 @@ static void Evaluate_CompositeFunction(benchmark::State& state) {
size_t stopCount = state.range(0);
auto doc = createFunctionJSON(stopCount);
conversion::Error error;
optional<CompositeFunction<float>> function = conversion::convertJSON<CompositeFunction<float>>(doc, error);
optional<DataDrivenPropertyValue<float>> function = conversion::convertJSON<DataDrivenPropertyValue<float>>(doc, error);
if (!function) {
state.SkipWithError(error.message.c_str());
}

while(state.KeepRunning()) {
float z = 24.0f * static_cast<float>(rand() % 100) / 100;
function->evaluate(z, StubGeometryTileFeature(PropertyMap { { "x", static_cast<int64_t>(rand() % 100) } }), -1.0f);
function->asExpression().evaluate(z, StubGeometryTileFeature(PropertyMap { { "x", static_cast<int64_t>(rand() % 100) } }), -1.0f);
}

state.SetLabel(std::to_string(stopCount).c_str());
Expand Down
10 changes: 4 additions & 6 deletions benchmark/function/source_function.benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

#include <mbgl/benchmark/stub_geometry_tile_feature.hpp>

#include <mbgl/style/function/source_function.hpp>

#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/json.hpp>
#include <mbgl/style/conversion/function.hpp>
#include <mbgl/style/conversion/data_driven_property_value.hpp>

using namespace mbgl;
using namespace mbgl::style;
Expand All @@ -30,7 +28,7 @@ static void Parse_SourceFunction(benchmark::State& state) {
state.PauseTiming();
auto doc = createFunctionJSON(stopCount);
state.ResumeTiming();
optional<SourceFunction<float>> result = conversion::convertJSON<SourceFunction<float>>(doc, error);
optional<DataDrivenPropertyValue<float>> result = conversion::convertJSON<DataDrivenPropertyValue<float>>(doc, error);
if (!result) {
state.SkipWithError(error.message.c_str());
}
Expand All @@ -42,13 +40,13 @@ static void Evaluate_SourceFunction(benchmark::State& state) {
size_t stopCount = state.range(0);
auto doc = createFunctionJSON(stopCount);
conversion::Error error;
optional<SourceFunction<float>> function = conversion::convertJSON<SourceFunction<float>>(doc, error);
optional<DataDrivenPropertyValue<float>> function = conversion::convertJSON<DataDrivenPropertyValue<float>>(doc, error);
if (!function) {
state.SkipWithError(error.message.c_str());
}

while(state.KeepRunning()) {
function->evaluate(StubGeometryTileFeature(PropertyMap { { "x", static_cast<int64_t>(rand() % 100) } }), -1.0f);
function->asExpression().evaluate(StubGeometryTileFeature(PropertyMap { { "x", static_cast<int64_t>(rand() % 100) } }), -1.0f);
}

state.SetLabel(std::to_string(stopCount).c_str());
Expand Down
4 changes: 1 addition & 3 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ set(MBGL_CORE_FILES
include/mbgl/style/layer_type.hpp
include/mbgl/style/light.hpp
include/mbgl/style/position.hpp
include/mbgl/style/property_expression.hpp
include/mbgl/style/property_value.hpp
include/mbgl/style/source.hpp
include/mbgl/style/style.hpp
Expand Down Expand Up @@ -499,9 +500,6 @@ set(MBGL_CORE_FILES
src/mbgl/style/expression/value.cpp

# style/function
include/mbgl/style/function/camera_function.hpp
include/mbgl/style/function/composite_function.hpp
include/mbgl/style/function/source_function.hpp
src/mbgl/style/function/expression.cpp

# style/layers
Expand Down
6 changes: 1 addition & 5 deletions cmake/test-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ set(MBGL_TEST_FILES
# style
test/style/filter.test.cpp
test/style/properties.test.cpp
test/style/property_expression.test.cpp
test/style/source.test.cpp
test/style/style.test.cpp
test/style/style_image.test.cpp
Expand All @@ -87,11 +88,6 @@ set(MBGL_TEST_FILES
test/style/expression/expression.test.cpp
test/style/expression/util.test.cpp

# style/function
test/style/function/camera_function.test.cpp
test/style/function/composite_function.test.cpp
test/style/function/source_function.test.cpp

# test
test/include/mbgl/test.hpp
test/src/mbgl/test/fake_file_source.hpp
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/style/color_ramp_property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <mbgl/util/variant.hpp>
#include <mbgl/style/undefined.hpp>
#include <mbgl/style/function/camera_function.hpp>
#include <mbgl/style/expression/expression.hpp>

namespace mbgl {
namespace style {
Expand Down
2 changes: 0 additions & 2 deletions include/mbgl/style/conversion/color_ramp_property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include <mbgl/style/color_ramp_property_value.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion/function.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/find_zoom_curve.hpp>
#include <mbgl/style/expression/parsing_context.hpp>

namespace mbgl {
Expand Down
64 changes: 22 additions & 42 deletions include/mbgl/style/conversion/data_driven_property_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,54 @@
#include <mbgl/style/conversion/function.hpp>
#include <mbgl/style/expression/is_expression.hpp>
#include <mbgl/style/expression/is_constant.hpp>
#include <mbgl/style/expression/find_zoom_curve.hpp>
#include <mbgl/style/expression/literal.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/parsing_context.hpp>

#include <unordered_set>


namespace mbgl {
namespace style {
namespace conversion {

template <class T>
struct Converter<DataDrivenPropertyValue<T>> {

optional<DataDrivenPropertyValue<T>> operator()(const Convertible& value, Error& error) const {
using namespace mbgl::style::expression;

if (isUndefined(value)) {
return DataDrivenPropertyValue<T>();
} else if (isExpression(value)) {
}

optional<PropertyExpression<T>> expression;

if (isExpression(value)) {
ParsingContext ctx(valueTypeToExpressionType<T>());
ParseResult expression = ctx.parseLayerPropertyExpression(value);
if (!expression) {
ParseResult parsed = ctx.parseLayerPropertyExpression(value);
if (!parsed) {
error = { ctx.getCombinedErrors() };
return {};
}

bool featureConstant = isFeatureConstant(**expression);
bool zoomConstant = isZoomConstant(**expression);

if (featureConstant && !zoomConstant) {
return DataDrivenPropertyValue<T>(CameraFunction<T>(std::move(*expression)));
} else if (!featureConstant && zoomConstant) {
return DataDrivenPropertyValue<T>(SourceFunction<T>(std::move(*expression)));
} else if (!featureConstant && !zoomConstant) {
return DataDrivenPropertyValue<T>(CompositeFunction<T>(std::move(*expression)));
} else {
auto literal = dynamic_cast<Literal*>(expression->get());
assert(literal);
optional<T> constant = fromExpressionValue<T>(literal->getValue());
if (!constant) {
return {};
}
return DataDrivenPropertyValue<T>(*constant);
}
} else if (!isObject(value)) {
expression = PropertyExpression<T>(std::move(*parsed));
} else if (isObject(value)) {
expression = convertFunctionToExpression<T>(value, error);
} else {
optional<T> constant = convert<T>(value, error);
if (!constant) {
return {};
}
return DataDrivenPropertyValue<T>(*constant);
} else if (!objectMember(value, "property")) {
optional<CameraFunction<T>> function = convert<CameraFunction<T>>(value, error);
if (!function) {
return {};
}
return DataDrivenPropertyValue<T>(*function);
}

if (!expression) {
return {};
} else if (!(*expression).isFeatureConstant() || !(*expression).isZoomConstant()) {
return { std::move(*expression) };
} else {
optional<CompositeFunction<T>> composite = convert<CompositeFunction<T>>(value, error);
if (composite) {
return DataDrivenPropertyValue<T>(*composite);
}
optional<SourceFunction<T>> source = convert<SourceFunction<T>>(value, error);
if (!source) {
optional<T> constant = fromExpressionValue<T>(
dynamic_cast<const Literal&>((*expression).getExpression()).getValue());
if (!constant) {
return {};
}
return DataDrivenPropertyValue<T>(*source);
return DataDrivenPropertyValue<T>(*constant);
}
}
};
Expand Down
66 changes: 13 additions & 53 deletions include/mbgl/style/conversion/function.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <mbgl/style/function/camera_function.hpp>
#include <mbgl/style/function/source_function.hpp>
#include <mbgl/style/function/composite_function.hpp>
#include <mbgl/style/property_expression.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/expression/expression.hpp>
Expand All @@ -12,66 +10,28 @@ namespace mbgl {
namespace style {
namespace conversion {

template <class T>
optional<optional<T>> convertDefaultValue(const Convertible& value, Error& error) {
auto defaultValueValue = objectMember(value, "default");
if (!defaultValueValue) {
return optional<T>();
}
optional<std::unique_ptr<expression::Expression>> convertFunctionToExpression(expression::type::Type, const Convertible&, Error&);

auto defaultValue = convert<T>(*defaultValueValue, error);
if (!defaultValue) {
error = { R"(wrong type for "default": )" + error.message };
template <class T>
optional<PropertyExpression<T>> convertFunctionToExpression(const Convertible& value, Error& error) {
auto expression = convertFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error);
if (!expression) {
return {};
}

return { *defaultValue };
}

optional<std::unique_ptr<expression::Expression>> convertCameraFunctionToExpression(expression::type::Type, const Convertible&, Error&);
optional<std::unique_ptr<expression::Expression>> convertSourceFunctionToExpression(expression::type::Type, const Convertible&, Error&);
optional<std::unique_ptr<expression::Expression>> convertCompositeFunctionToExpression(expression::type::Type, const Convertible&, Error&);
optional<T> defaultValue;

template <class T>
struct Converter<CameraFunction<T>> {
optional<CameraFunction<T>> operator()(const Convertible& value, Error& error) const {
auto expression = convertCameraFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error);
if (!expression) {
return {};
}
return CameraFunction<T>(std::move(*expression), false);
}
};

template <class T>
struct Converter<SourceFunction<T>> {
optional<SourceFunction<T>> operator()(const Convertible& value, Error& error) const {
auto expression = convertSourceFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error);
if (!expression) {
return {};
}
auto defaultValue = convertDefaultValue<T>(value, error);
auto defaultValueValue = objectMember(value, "default");
if (defaultValueValue) {
defaultValue = convert<T>(*defaultValueValue, error);
if (!defaultValue) {
error = { R"(wrong type for "default": )" + error.message };
return {};
}
return SourceFunction<T>(std::move(*expression), *defaultValue);
}
};

template <class T>
struct Converter<CompositeFunction<T>> {
optional<CompositeFunction<T>> operator()(const Convertible& value, Error& error) const {
auto expression = convertCompositeFunctionToExpression(expression::valueTypeToExpressionType<T>(), value, error);
if (!expression) {
return {};
}
auto defaultValue = convertDefaultValue<T>(value, error);
if (!defaultValue) {
return {};
}
return CompositeFunction<T>(std::move(*expression), *defaultValue);
}
};
return PropertyExpression<T>(std::move(*expression), defaultValue);
}

} // namespace conversion
} // namespace style
Expand Down
Loading

0 comments on commit 2e55305

Please sign in to comment.