add min/max functions and refactor FunctionProperty

fixes #83
This commit is contained in:
Konstantin Käfer
2014-03-26 12:21:33 +01:00
parent 700dcc9260
commit 0c6f993695
7 changed files with 69 additions and 142 deletions

View File

@@ -240,6 +240,7 @@ module.exports = {
"opacity": [ "linear", 15, 0, 1.0, 0, 0.75 ]
},
"restaurant_poi": {
"enabled": [ "min", 18 ],
"type": "point",
"color": "#cccccc",
"size": 18,

View File

@@ -34,40 +34,31 @@ enum class Property {
namespace functions {
float null(float z, const std::vector<float>&);
bool null(float z, const std::vector<bool>&);
float constant(float z, const std::vector<float>& values);
bool constant(float z, const std::vector<bool>& values);
float min(float z, const std::vector<float>& values);
float max(float z, const std::vector<float>& values);
float stops(float z, const std::vector<float>& values);
bool stops(float z, const std::vector<bool>& values);
float linear(float z, const std::vector<float>& values);
bool linear(float z, const std::vector<bool>& values);
float exponential(float z, const std::vector<float>& values);
bool exponential(float z, const std::vector<bool>& values);
}
template <typename T>
struct FunctionProperty {
typedef T (*fn)(float z, const std::vector<T>& values);
typedef float (*fn)(float z, const std::vector<float>& values);
fn function;
std::vector<T> values;
std::vector<float> values;
inline FunctionProperty() : function(&functions::null) {}
inline FunctionProperty(T value) : function(&functions::constant), values(1, value) {}
inline T operator()(float z) const { return function(z, values); }
inline FunctionProperty(float value) : function(&functions::constant), values(1, value) {}
template <typename T> inline T evaluate(float z) const { return function(z, values); }
};
struct PointClass {
FunctionProperty<bool> enabled = true;
FunctionProperty<float> size;
FunctionProperty enabled = true;
FunctionProperty size;
Color color = {{ 0, 0, 0, 1 }};
FunctionProperty<float> opacity = 1;
FunctionProperty opacity = 1;
std::string image;
};
@@ -80,11 +71,11 @@ struct PointProperties {
};
struct LineClass {
FunctionProperty<bool> enabled = true;
FunctionProperty<float> width;
FunctionProperty<float> offset;
FunctionProperty enabled = true;
FunctionProperty width;
FunctionProperty offset;
Color color = {{ 0, 0, 0, 1 }};
FunctionProperty<float> opacity = 1;
FunctionProperty opacity = 1;
};
struct LineProperties {
@@ -96,12 +87,12 @@ struct LineProperties {
};
struct FillClass {
FunctionProperty<bool> enabled = true;
FunctionProperty enabled = true;
Winding winding = Winding::NonZero;
FunctionProperty<bool> antialias = true;
FunctionProperty antialias = true;
Color fill_color = {{ 0, 0, 0, 1 }};
Color stroke_color = {{ 0, 0, 0, std::numeric_limits<float>::infinity() }};
FunctionProperty<float> opacity = 1;
FunctionProperty opacity = 1;
std::string image;
};
@@ -116,13 +107,13 @@ struct FillProperties {
};
struct TextClass {
FunctionProperty<bool> enabled = true;
FunctionProperty enabled = true;
Color color = {{ 0, 0, 0, 1 }};
Color halo = {{ 1, 1, 1, 0.75 }};
FunctionProperty<float> haloRadius = 0.25f;
FunctionProperty<float> size = 12.0f;
FunctionProperty<float> rotate = 0.0f;
FunctionProperty<bool> alwaysVisible = false;
FunctionProperty haloRadius = 0.25f;
FunctionProperty size = 12.0f;
FunctionProperty rotate = 0.0f;
FunctionProperty alwaysVisible = false;
};
struct TextProperties {

View File

@@ -30,16 +30,6 @@ public:
void cascade(float z);
private:
static std::pair<std::string, BucketDescription> parseBucket(pbf data);
static LayerDescription parseLayer(pbf data);
static std::pair<std::string, ClassDescription> parseClass(pbf data);
static std::pair<std::string, FillClass> parseFillClass(pbf data);
static std::pair<std::string, LineClass> parseLineClass(pbf data);
static std::pair<std::string, PointClass> parsePointClass(pbf data);
template <typename T> static FunctionProperty<T> parseProperty(pbf data);
static Color parseColor(pbf& data);
public:
std::shared_ptr<Sprite> sprite;

View File

@@ -30,9 +30,8 @@ private:
std::string parseString(JSVal value);
Color parseColor(JSVal value);
Value parseValue(JSVal value);
template <typename T> typename FunctionProperty<T>::fn parseFunctionType(JSVal type);
FunctionProperty<float> parseFloatFunction(JSVal value);
FunctionProperty<bool> parseBoolFunction(JSVal value);
FunctionProperty::fn parseFunctionType(JSVal type);
FunctionProperty parseFunction(JSVal value);
private:
std::map<std::string, const rapidjson::Value *> constants;

View File

@@ -12,20 +12,20 @@ float functions::null(float z, const std::vector<float>&) {
return 0;
}
bool functions::null(float z, const std::vector<bool>&) {
return false;
}
float functions::constant(float z, const std::vector<float>& values) {
assert(values.size() == 1);
return values.front();
}
bool functions::constant(float z, const std::vector<bool>& values) {
float functions::min(float z, const std::vector<float>& values) {
assert(values.size() == 1);
return values.front();
return z >= values.front();
}
float functions::max(float z, const std::vector<float>& values) {
assert(values.size() == 1);
return z <= values.front();
}
float functions::stops(float z, const std::vector<float>& stops) {
// We need an even number of stop values.
@@ -66,11 +66,6 @@ float functions::stops(float z, const std::vector<float>& stops) {
}
}
bool functions::stops(float z, const std::vector<bool>& values) {
// TODO
return false;
}
float functions::linear(float z, const std::vector<float>& values) {
if (values.size() != 5) {
return 0;
@@ -85,11 +80,6 @@ float functions::linear(float z, const std::vector<float>& values) {
return fmin(fmax(min, val + (z - z_base) * slope), max);
}
bool functions::linear(float z, const std::vector<bool>&) {
// TODO
return false;
}
float functions::exponential(float z, const std::vector<float>& values) {
if (values.size() != 5) {
return 0;
@@ -102,10 +92,4 @@ float functions::exponential(float z, const std::vector<float>& values) {
const float max = values[4];
return fmin(fmax(min, val + pow(1.75, (z - z_base)) * slope), max);
// return fmin(fmax(min, val + (z - z_base) * slope), max);
}
bool functions::exponential(float z, const std::vector<bool>&) {
// TODO
return false;
}

View File

@@ -37,12 +37,12 @@ void Style::cascade(float z) {
// TODO: This should be restricted to fill styles that have actual
// values so as to not override with default values.
llmr::FillProperties& fill = computed.fills[layer_name];
fill.enabled = layer.enabled(z);
fill.enabled = layer.enabled.evaluate<bool>(z);
fill.winding = layer.winding;
fill.antialias = layer.antialias(z);
fill.antialias = layer.antialias.evaluate<bool>(z);
fill.fill_color = layer.fill_color;
fill.stroke_color = layer.stroke_color;
fill.opacity = layer.opacity(z);
fill.opacity = layer.opacity.evaluate<float>(z);
fill.image = layer.image;
}
@@ -54,11 +54,11 @@ void Style::cascade(float z) {
// TODO: This should be restricted to line styles that have actual
// values so as to not override with default values.
llmr::LineProperties& stroke = computed.lines[layer_name];
stroke.enabled = layer.enabled(z);
stroke.width = layer.width(z);
stroke.offset = layer.offset(z);
stroke.enabled = layer.enabled.evaluate<bool>(z);
stroke.width = layer.width.evaluate<float>(z);
stroke.offset = layer.offset.evaluate<float>(z);
stroke.color = layer.color;
stroke.opacity = layer.opacity(z);
stroke.opacity = layer.opacity.evaluate<float>(z);
}
// Cascade point classes
@@ -69,10 +69,10 @@ void Style::cascade(float z) {
// TODO: This should be restricted to point styles that have actual
// values so as to not override with default values.
llmr::PointProperties& point = computed.points[layer_name];
point.enabled = layer.enabled(z);
point.enabled = layer.enabled.evaluate<bool>(z);
point.color = layer.color;
point.size = layer.size(z);
point.opacity = layer.opacity(z);
point.size = layer.size.evaluate<float>(z);
point.opacity = layer.opacity.evaluate<float>(z);
point.image = layer.image;
}
@@ -83,13 +83,13 @@ void Style::cascade(float z) {
// TODO: This should be restricted to point styles that have actual
// values so as to not override with default values.
llmr::TextProperties& text = computed.texts[layer_name];
text.enabled = layer.enabled(z);
text.enabled = layer.enabled.evaluate<bool>(z);
text.color = layer.color;
text.size = layer.size(z);
text.size = layer.size.evaluate<float>(z);
text.halo = layer.halo;
text.haloRadius = layer.haloRadius(z);
text.rotate = layer.rotate(z);
text.alwaysVisible = layer.alwaysVisible(z);
text.haloRadius = layer.haloRadius.evaluate<float>(z);
text.rotate = layer.rotate.evaluate<float>(z);
text.alwaysVisible = layer.alwaysVisible.evaluate<bool>(z);
}
// Cascade background

View File

@@ -319,8 +319,7 @@ Color StyleParser::parseColor(JSVal value) {
}
}
template <typename T>
typename FunctionProperty<T>::fn StyleParser::parseFunctionType(JSVal type) {
FunctionProperty::fn StyleParser::parseFunctionType(JSVal type) {
if (type.IsString()) {
std::string t { type.GetString(), type.GetStringLength() };
if (t == "constant") {
@@ -331,6 +330,10 @@ typename FunctionProperty<T>::fn StyleParser::parseFunctionType(JSVal type) {
return &functions::stops;
} else if (t == "exponential") {
return &functions::exponential;
} else if (t == "min") {
return &functions::min;
} else if (t == "max") {
return &functions::max;
} else {
throw Style::exception("unknown function type");
}
@@ -339,15 +342,15 @@ typename FunctionProperty<T>::fn StyleParser::parseFunctionType(JSVal type) {
}
}
FunctionProperty<float> StyleParser::parseFloatFunction(JSVal value) {
FunctionProperty<float> property;
FunctionProperty StyleParser::parseFunction(JSVal value) {
FunctionProperty property;
if (value.IsArray()) {
if (value.Size() < 1) {
throw Style::exception("value function does not have arguments");
}
property.function = parseFunctionType<float>(value[(rapidjson::SizeType)0]);
property.function = parseFunctionType(value[(rapidjson::SizeType)0]);
for (rapidjson::SizeType i = 1; i < value.Size(); ++i) {
JSVal stop = value[i];
if (stop.IsObject()) {
@@ -370,8 +373,10 @@ FunctionProperty<float> StyleParser::parseFloatFunction(JSVal value) {
property.values.push_back(val.GetDouble());
} else if (stop.IsNumber()) {
property.values.push_back(stop.GetDouble());
} else if (stop.IsBool()) {
property.values.push_back(stop.GetBool());
} else {
throw Style::exception("stop must be a number");
throw Style::exception("function argument must be a numeric value");
}
}
} else if (value.IsNumber()) {
@@ -382,54 +387,11 @@ FunctionProperty<float> StyleParser::parseFloatFunction(JSVal value) {
return property;
}
FunctionProperty<bool> StyleParser::parseBoolFunction(JSVal value) {
FunctionProperty<bool> property;
if (value.IsArray()) {
if (value.Size() < 1) {
throw Style::exception("value function does not have arguments");
}
property.function = parseFunctionType<bool>(value[(rapidjson::SizeType)0]);
for (rapidjson::SizeType i = 1; i < value.Size(); ++i) {
JSVal stop = value[i];
if (stop.IsObject()) {
if (!stop.HasMember("z")) {
throw Style::exception("stop must have zoom level specification");
}
JSVal z = stop["z"];
if (!z.IsBool()) {
throw Style::exception("zoom level in stops must be a number");
}
property.values.push_back(z.GetBool());
if (!stop.HasMember("val")) {
throw Style::exception("stop must have value specification");
}
JSVal val = stop["val"];
if (!val.IsBool()) {
throw Style::exception("value in stops must be a number");
}
property.values.push_back(val.GetBool());
} else if (stop.IsBool()) {
property.values.push_back(stop.GetBool());
} else {
throw Style::exception("stop must be a number");
}
}
} else if (value.IsBool()) {
property.function = &functions::constant;
property.values.push_back(value.GetBool());
}
return property;
}
FillClass StyleParser::parseFillClass(JSVal value) {
FillClass klass;
if (value.HasMember("enabled")) {
klass.enabled = parseBoolFunction(value["enabled"]);
klass.enabled = parseFunction(value["enabled"]);
}
if (value.HasMember("color")) {
@@ -451,7 +413,7 @@ FillClass StyleParser::parseFillClass(JSVal value) {
}
if (value.HasMember("opacity")) {
klass.opacity = parseFloatFunction(value["opacity"]);
klass.opacity = parseFunction(value["opacity"]);
}
return klass;
@@ -461,7 +423,7 @@ LineClass StyleParser::parseLineClass(JSVal value) {
LineClass klass;
if (value.HasMember("enabled")) {
klass.enabled = parseBoolFunction(value["enabled"]);
klass.enabled = parseFunction(value["enabled"]);
}
if (value.HasMember("color")) {
@@ -469,11 +431,11 @@ LineClass StyleParser::parseLineClass(JSVal value) {
}
if (value.HasMember("width")) {
klass.width = parseFloatFunction(value["width"]);
klass.width = parseFunction(value["width"]);
}
if (value.HasMember("opacity")) {
klass.opacity = parseFloatFunction(value["opacity"]);
klass.opacity = parseFunction(value["opacity"]);
}
return klass;
@@ -483,7 +445,7 @@ PointClass StyleParser::parsePointClass(JSVal value) {
PointClass klass;
if (value.HasMember("enabled")) {
klass.enabled = parseBoolFunction(value["enabled"]);
klass.enabled = parseFunction(value["enabled"]);
}
if (value.HasMember("color")) {
@@ -491,7 +453,7 @@ PointClass StyleParser::parsePointClass(JSVal value) {
}
if (value.HasMember("opacity")) {
klass.opacity = parseFloatFunction(value["opacity"]);
klass.opacity = parseFunction(value["opacity"]);
}
if (value.HasMember("image")) {
@@ -499,7 +461,7 @@ PointClass StyleParser::parsePointClass(JSVal value) {
}
if (value.HasMember("size")) {
klass.size = parseFloatFunction(value["size"]);
klass.size = parseFunction(value["size"]);
}
return klass;
@@ -510,7 +472,7 @@ TextClass StyleParser::parseTextClass(JSVal value) {
TextClass klass;
if (value.HasMember("enabled")) {
klass.enabled = parseBoolFunction(value["enabled"]);
klass.enabled = parseFunction(value["enabled"]);
}
if (value.HasMember("color")) {
@@ -522,19 +484,19 @@ TextClass StyleParser::parseTextClass(JSVal value) {
}
if (value.HasMember("strokeWidth")) {
klass.haloRadius = parseFloatFunction(value["strokeWidth"]);
klass.haloRadius = parseFunction(value["strokeWidth"]);
}
if (value.HasMember("size")) {
klass.size = parseFloatFunction(value["size"]);
klass.size = parseFunction(value["size"]);
}
if (value.HasMember("rotate")) {
klass.rotate = parseFloatFunction(value["rotate"]);
klass.rotate = parseFunction(value["rotate"]);
}
if (value.HasMember("alwaysVisible")) {
klass.alwaysVisible = parseBoolFunction(value["alwaysVisible"]);
klass.alwaysVisible = parseFunction(value["alwaysVisible"]);
}
return klass;