Merge branch 'master' into fuzz-pipe
Conflicts: src/style/style_parser.cpp
This commit is contained in:
@@ -6,82 +6,36 @@
|
||||
namespace mbgl {
|
||||
|
||||
|
||||
template <>
|
||||
bool LinearFunction<bool>::evaluate(float z) const {
|
||||
return z < z_base ? slope >= 0 : z > z_base ? slope >= 0 : value;
|
||||
}
|
||||
|
||||
template <>
|
||||
float LinearFunction<float>::evaluate(float z) const {
|
||||
return std::fmin(std::fmax(min, value + (z - z_base) * slope), max);
|
||||
}
|
||||
|
||||
template <>
|
||||
Color LinearFunction<Color>::evaluate(float z) const {
|
||||
return {{
|
||||
std::fmin(std::fmax(min[0], value[0] + (z - z_base) * slope), max[0]),
|
||||
std::fmin(std::fmax(min[1], value[1] + (z - z_base) * slope), max[1]),
|
||||
std::fmin(std::fmax(min[2], value[2] + (z - z_base) * slope), max[2]),
|
||||
std::fmin(std::fmax(min[3], value[3] + (z - z_base) * slope), max[3])
|
||||
}};
|
||||
}
|
||||
|
||||
|
||||
template <>
|
||||
bool ExponentialFunction<bool>::evaluate(float z) const {
|
||||
return z < z_base ? slope >= 0 : z > z_base ? slope >= 0 : value;
|
||||
}
|
||||
|
||||
template <>
|
||||
float ExponentialFunction<float>::evaluate(float z) const {
|
||||
return std::fmin(std::fmax(min, value + std::pow(exp_base, (z - z_base)) * slope), max);
|
||||
}
|
||||
|
||||
template <>
|
||||
Color ExponentialFunction<Color>::evaluate(float z) const {
|
||||
return {{
|
||||
std::fmin(std::fmax(min[0], value[0] + float(std::pow(exp_base, (z - z_base))) * slope), max[0]),
|
||||
std::fmin(std::fmax(min[1], value[1] + float(std::pow(exp_base, (z - z_base))) * slope), max[1]),
|
||||
std::fmin(std::fmax(min[2], value[2] + float(std::pow(exp_base, (z - z_base))) * slope), max[2]),
|
||||
std::fmin(std::fmax(min[3], value[3] + float(std::pow(exp_base, (z - z_base))) * slope), max[3])
|
||||
}};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T exponentialInterpolate(T smaller, T larger, const float factor);
|
||||
inline T interpolate(T smaller, T larger, const float factor);
|
||||
|
||||
template <>
|
||||
inline float exponentialInterpolate(const float smaller, const float larger, const float factor) {
|
||||
// Linear interpolation if base is 0
|
||||
if (smaller == 0.0f) {
|
||||
return factor * larger;
|
||||
}
|
||||
// Exponential interpolation between the values
|
||||
return smaller * std::pow(larger / smaller, factor);
|
||||
inline float interpolate(const float smaller, const float larger, const float factor) {
|
||||
return (smaller * (1 - factor)) + (larger * factor);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool exponentialInterpolate(const bool smaller, const bool larger, const float factor) {
|
||||
return exponentialInterpolate(float(smaller), float(larger), factor);
|
||||
inline bool interpolate(const bool smaller, const bool larger, const float factor) {
|
||||
return interpolate(float(smaller), float(larger), factor);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline Color exponentialInterpolate(const Color smaller, const Color larger, const float factor) {
|
||||
inline Color interpolate(const Color smaller, const Color larger, const float factor) {
|
||||
return {{
|
||||
exponentialInterpolate(smaller[0], larger[0], factor),
|
||||
exponentialInterpolate(smaller[1], larger[1], factor),
|
||||
exponentialInterpolate(smaller[2], larger[2], factor),
|
||||
exponentialInterpolate(smaller[3], larger[3], factor)
|
||||
interpolate(smaller[0], larger[0], factor),
|
||||
interpolate(smaller[1], larger[1], factor),
|
||||
interpolate(smaller[2], larger[2], factor),
|
||||
interpolate(smaller[3], larger[3], factor)
|
||||
}};
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
T exponentialDefault();
|
||||
inline T defaultStopsValue();
|
||||
|
||||
template <> bool exponentialDefault() { return true; }
|
||||
template <> float exponentialDefault() { return 1.0f; }
|
||||
template <> Color exponentialDefault() { return {{ 0, 0, 0, 1 }}; }
|
||||
template <> inline bool defaultStopsValue() { return true; }
|
||||
template <> inline float defaultStopsValue() { return 1.0f; }
|
||||
template <> inline Color defaultStopsValue() { return {{ 0, 0, 0, 1 }}; }
|
||||
|
||||
|
||||
template <typename T>
|
||||
@@ -112,15 +66,22 @@ T StopsFunction<T>::evaluate(float z) const {
|
||||
if (larger_z == smaller_z || larger_val == smaller_val) {
|
||||
return smaller_val;
|
||||
}
|
||||
float factor = (z - smaller_z) / (larger_z - smaller_z);
|
||||
return exponentialInterpolate<T>(smaller_val, larger_val, factor);
|
||||
const float zoomDiff = larger_z - smaller_z;
|
||||
const float zoomProgress = z - smaller_z;
|
||||
if (base == 1.0f) {
|
||||
const float t = zoomProgress / zoomDiff;
|
||||
return interpolate<T>(smaller_val, larger_val, t);
|
||||
} else {
|
||||
const float t = (std::pow(base, zoomProgress) - 1) / (std::pow(base, zoomDiff) - 1);
|
||||
return interpolate<T>(smaller_val, larger_val, t);
|
||||
}
|
||||
} else if (larger) {
|
||||
return larger_val;
|
||||
} else if (smaller) {
|
||||
return smaller_val;
|
||||
} else {
|
||||
// No stop defined.
|
||||
return exponentialDefault<T>();
|
||||
return defaultStopsValue<T>();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -231,95 +231,56 @@ Color StyleParser::parseFunctionArgument(JSVal value) {
|
||||
return parseColor(rvalue);
|
||||
}
|
||||
|
||||
template <typename T> inline float defaultBaseValue() { return 1.75; }
|
||||
template <> inline float defaultBaseValue<Color>() { return 1.0; }
|
||||
|
||||
template <typename T>
|
||||
std::tuple<bool, Function<T>> StyleParser::parseFunction(JSVal value) {
|
||||
if (!value.HasMember("fn")) {
|
||||
Log::Warning(Event::ParseStyle, "function must specify a function name");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
JSVal value_fn = value["fn"];
|
||||
if (!value_fn.IsString()) {
|
||||
if (!value.HasMember("stops")) {
|
||||
Log::Warning(Event::ParseStyle, "function must specify a function type");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
const std::string type { value_fn.GetString(), value_fn.GetStringLength() };
|
||||
float base = defaultBaseValue<T>();
|
||||
|
||||
if (type == "linear") {
|
||||
if (!value.HasMember("z")) {
|
||||
Log::Warning(Event::ParseStyle, "linear function must specify a base zoom level");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
if (value.HasMember("base")) {
|
||||
JSVal value_base = value["base"];
|
||||
if (value_base.IsNumber()) {
|
||||
base = value_base.GetDouble();
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "base must be numeric");
|
||||
}
|
||||
if (!value.HasMember("val")) {
|
||||
Log::Warning(Event::ParseStyle, "linear function must specify a base value");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
const float z_base = parseFunctionArgument<float>(value["z"]);
|
||||
const T val = parseFunctionArgument<T>(value["val"]);
|
||||
const float slope = value.HasMember("slope") ? parseFunctionArgument<float>(value["slope"]) : 1;
|
||||
const T min = value.HasMember("min") ? parseFunctionArgument<T>(value["min"]) : T();
|
||||
const T max = value.HasMember("max") ? parseFunctionArgument<T>(value["max"]) : T();
|
||||
return std::tuple<bool, Function<T>> { true, LinearFunction<T>(val, z_base, slope, min, max) };
|
||||
}
|
||||
else if (type == "exponential") {
|
||||
if (!value.HasMember("z")) {
|
||||
Log::Warning(Event::ParseStyle, "exponential function must specify a base zoom level");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
if (!value.HasMember("val")) {
|
||||
Log::Warning(Event::ParseStyle, "exponential function must specify a base value");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
const float z_base = parseFunctionArgument<float>(value["z"]);
|
||||
const float exp_base = value.HasMember("base") ? parseFunctionArgument<float>(value["base"]) : 1.75;
|
||||
const T val = parseFunctionArgument<T>(value["val"]);
|
||||
const float slope = value.HasMember("slope") ? parseFunctionArgument<float>(value["slope"]) : 1;
|
||||
const T min = value.HasMember("min") ? parseFunctionArgument<T>(value["min"]) : T();
|
||||
const T max = value.HasMember("max") ? parseFunctionArgument<T>(value["max"]) : T();
|
||||
return std::tuple<bool, Function<T>> { true, ExponentialFunction<T>(val, z_base, exp_base, slope, min, max) };
|
||||
|
||||
JSVal value_stops = value["stops"];
|
||||
if (!value_stops.IsArray()) {
|
||||
Log::Warning(Event::ParseStyle, "stops function must specify a stops array");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
else if (type == "stops") {
|
||||
if (!value.HasMember("stops")) {
|
||||
Log::Warning(Event::ParseStyle, "stops function must specify a stops array");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
JSVal value_stops = value["stops"];
|
||||
if (!value_stops.IsArray()) {
|
||||
Log::Warning(Event::ParseStyle, "stops function must specify a stops array");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
std::vector<std::pair<float, T>> stops;
|
||||
for (rapidjson::SizeType i = 0; i < value_stops.Size(); ++i) {
|
||||
JSVal stop = value_stops[i];
|
||||
if (stop.IsArray()) {
|
||||
if (stop.Size() != 2) {
|
||||
Log::Warning(Event::ParseStyle, "stop must have zoom level and value specification");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
JSVal z = stop[rapidjson::SizeType(0)];
|
||||
if (!z.IsNumber()) {
|
||||
Log::Warning(Event::ParseStyle, "zoom level in stop must be a number");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
stops.emplace_back(z.GetDouble(), parseFunctionArgument<T>(stop[rapidjson::SizeType(1)]));
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "function argument must be a numeric value");
|
||||
std::vector<std::pair<float, T>> stops;
|
||||
for (rapidjson::SizeType i = 0; i < value_stops.Size(); ++i) {
|
||||
JSVal stop = value_stops[i];
|
||||
if (stop.IsArray()) {
|
||||
if (stop.Size() != 2) {
|
||||
Log::Warning(Event::ParseStyle, "stop must have zoom level and value specification");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
JSVal z = stop[rapidjson::SizeType(0)];
|
||||
if (!z.IsNumber()) {
|
||||
Log::Warning(Event::ParseStyle, "zoom level in stop must be a number");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
stops.emplace_back(z.GetDouble(), parseFunctionArgument<T>(stop[rapidjson::SizeType(1)]));
|
||||
} else {
|
||||
Log::Warning(Event::ParseStyle, "function argument must be a numeric value");
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
}
|
||||
|
||||
return std::tuple<bool, Function<T>> { true, StopsFunction<T>(stops) };
|
||||
}
|
||||
else {
|
||||
Log::Warning(Event::ParseStyle, "function type '%s' is unknown", type.c_str());
|
||||
}
|
||||
|
||||
return std::tuple<bool, Function<T>> { false, ConstantFunction<T>(T()) };
|
||||
return std::tuple<bool, Function<T>> { true, StopsFunction<T>(stops, base) };
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user