[core, ios, macos] Refactor composite stop types and conversions

This commit is contained in:
John Firebaugh
2017-02-07 12:35:27 -08:00
parent 9df24addfb
commit f35ca0d9df
13 changed files with 268 additions and 149 deletions

View File

@@ -155,7 +155,7 @@ public:
MGLCompositeStyleFunction<ObjCType> *compositeStyleFunction = (MGLCompositeStyleFunction<ObjCType> *)value;
switch (compositeStyleFunction.interpolationMode) {
case MGLInterpolationModeExponential: {
__block std::map<float, mbgl::style::ExponentialStops<MBGLType>> outerStops = {};
__block std::map<float, std::map<float, MBGLType>> outerStops = {};
[compositeStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSDictionary * _Nonnull stopValue, BOOL * _Nonnull stop) {
std::map<float, MBGLType> stops = {};
for (NSNumber *key in stopValue.allKeys) {
@@ -166,10 +166,12 @@ public:
NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
stops[key.floatValue] = mbglStopValue.asConstant();
}
mbgl::style::ExponentialStops<MBGLType> innerStops = {stops, (float)compositeStyleFunction.interpolationBase};
outerStops[zoomKey.floatValue] = innerStops;
outerStops[zoomKey.floatValue] = stops;
}];
mbgl::style::CompositeFunction<MBGLType> compositeFunction = {compositeStyleFunction.attributeName.UTF8String, outerStops};
mbgl::style::CompositeFunction<MBGLType> compositeFunction {
compositeStyleFunction.attributeName.UTF8String,
mbgl::style::CompositeExponentialStops<MBGLType> { outerStops, (float)compositeStyleFunction.interpolationBase }
};
if (compositeStyleFunction.defaultValue) {
NSCAssert([compositeStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
@@ -181,7 +183,7 @@ public:
}
break;
case MGLInterpolationModeInterval: {
__block std::map<float, mbgl::style::IntervalStops<MBGLType>> outerStops = {};
__block std::map<float, std::map<float, MBGLType>> outerStops = {};
[compositeStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSDictionary * _Nonnull stopValue, BOOL * _Nonnull stop) {
std::map<float, MBGLType> stops = {};
for (NSNumber *key in stopValue.allKeys) {
@@ -192,10 +194,12 @@ public:
NSCAssert(mbglStopValue.isConstant(), @"Stops must be constant");
stops[key.floatValue] = mbglStopValue.asConstant();
}
mbgl::style::IntervalStops<MBGLType> innerStops = {stops};
outerStops[zoomKey.floatValue] = innerStops;
outerStops[zoomKey.floatValue] = stops;
}];
mbgl::style::CompositeFunction<MBGLType> compositeFunction = {compositeStyleFunction.attributeName.UTF8String, outerStops};
mbgl::style::CompositeFunction<MBGLType> compositeFunction {
compositeStyleFunction.attributeName.UTF8String,
mbgl::style::CompositeIntervalStops<MBGLType> { outerStops }
};
if (compositeStyleFunction.defaultValue) {
NSCAssert([compositeStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
@@ -207,7 +211,7 @@ public:
}
break;
case MGLInterpolationModeCategorical: {
__block std::map<float, mbgl::style::CategoricalStops<MBGLType>> outerStops = {};
__block std::map<float, std::map<mbgl::style::CategoricalValue, MBGLType>> outerStops = {};
[compositeStyleFunction.stops enumerateKeysAndObjectsUsingBlock:^(NSNumber * _Nonnull zoomKey, NSDictionary * _Nonnull stopValue, BOOL * _Nonnull stop) {
__block std::map<mbgl::style::CategoricalValue, MBGLType> stops = {};
[stopValue enumerateKeysAndObjectsUsingBlock:^(id _Nonnull categoryKey, MGLStyleValue<ObjCType> * _Nonnull innerValue, BOOL * _Nonnull stop) {
@@ -233,10 +237,12 @@ public:
}
}
}];
mbgl::style::CategoricalStops<MBGLType> categoricalStops = {stops};
outerStops[zoomKey.floatValue] = categoricalStops;
outerStops[zoomKey.floatValue] = stops;
}];
mbgl::style::CompositeFunction<MBGLType> compositeFunction = {compositeStyleFunction.attributeName.UTF8String, outerStops};
mbgl::style::CompositeFunction<MBGLType> compositeFunction {
compositeStyleFunction.attributeName.UTF8String,
mbgl::style::CompositeCategoricalStops<MBGLType> { outerStops }
};
if (compositeStyleFunction.defaultValue) {
NSCAssert([compositeStyleFunction.defaultValue isKindOfClass:[MGLStyleConstantValue class]], @"Default value must be constant");
MBGLType mbglValue;
@@ -655,27 +661,25 @@ private: // Private utilities for converting from mbgl to mgl values
// Converts a composite function and all possible mbgl stops into an equivilant mgl style value
class CompositeFunctionStopsVisitor {
public:
id operator()(const std::map<float, mbgl::style::ExponentialStops<MBGLType>> &mbglStops) {
float base = 1.0f;
NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.size()];
for (auto const& outerStop: mbglStops) {
stops[@(outerStop.first)] = toConvertedStops(outerStop.second.stops);
base = outerStop.second.base;
id operator()(const mbgl::style::CompositeExponentialStops<MBGLType> &mbglStops) {
NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
for (auto const& outerStop: mbglStops.stops) {
stops[@(outerStop.first)] = toConvertedStops(outerStop.second);
}
MGLCompositeStyleFunction *compositeFunction = [MGLCompositeStyleFunction functionWithInterpolationMode:MGLInterpolationModeExponential
stops:stops
attributeName:@(mbglFunction.property.c_str())
options:@{MGLStyleFunctionOptionInterpolationBase: @(base)}];
options:@{MGLStyleFunctionOptionInterpolationBase: @(mbglStops.base)}];
if (mbglFunction.defaultValue) {
compositeFunction.defaultValue = [MGLStyleValue valueWithRawValue:toMGLRawStyleValue(*mbglFunction.defaultValue)];
}
return compositeFunction;
}
id operator()(const std::map<float, mbgl::style::IntervalStops<MBGLType>> &mbglStops) {
NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.size()];
for (auto const& outerStop: mbglStops) {
stops[@(outerStop.first)] = toConvertedStops(outerStop.second.stops);
id operator()(const mbgl::style::CompositeIntervalStops<MBGLType> &mbglStops) {
NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
for (auto const& outerStop: mbglStops.stops) {
stops[@(outerStop.first)] = toConvertedStops(outerStop.second);
}
MGLCompositeStyleFunction *compositeFunction = [MGLCompositeStyleFunction functionWithInterpolationMode:MGLInterpolationModeInterval
stops:stops
@@ -687,11 +691,11 @@ private: // Private utilities for converting from mbgl to mgl values
return compositeFunction;
}
id operator()(const std::map<float, mbgl::style::CategoricalStops<MBGLType>> &mbglStops) {
NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.size()];
for (auto const& outerStop: mbglStops) {
NSMutableDictionary *innerStops = [NSMutableDictionary dictionaryWithCapacity:outerStop.second.stops.size()];
for (const auto &mbglStop : outerStop.second.stops) {
id operator()(const mbgl::style::CompositeCategoricalStops<MBGLType> &mbglStops) {
NSMutableDictionary *stops = [NSMutableDictionary dictionaryWithCapacity:mbglStops.stops.size()];
for (auto const& outerStop: mbglStops.stops) {
NSMutableDictionary *innerStops = [NSMutableDictionary dictionaryWithCapacity:outerStop.second.size()];
for (const auto &mbglStop : outerStop.second) {
auto categoricalValue = mbglStop.first;
auto rawValue = toMGLRawStyleValue(mbglStop.second);
CategoricalValueVisitor categoricalValueVisitor;