Files
MapLibreParticleLab/src/mbgl/style/function_evaluator.cpp
John Firebaugh c4089b60bc [core] Extract Function::evaluation to a separate class (#4811)
This allows the Function<Faded<T>> partial specialization to be eliminated, giving all property functions a consistent storage type.
2016-04-25 09:51:47 -07:00

146 lines
5.4 KiB
C++

#include <mbgl/style/function_evaluator.hpp>
#include <mbgl/style/style_calculation_parameters.hpp>
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/chrono.hpp>
#include <cmath>
namespace mbgl {
template <typename T>
inline T defaultStopsValue();
template <> inline bool defaultStopsValue() { return true; }
template <> inline float defaultStopsValue() { return 1.0f; }
template <> inline Color defaultStopsValue() { return {{ 0, 0, 0, 1 }}; }
template <> inline std::vector<float> defaultStopsValue() { return {{ 1, 0 }}; }
template <> inline std::vector<std::string> defaultStopsValue() { return {{}}; }
template <> inline std::array<float, 2> defaultStopsValue() { return {{ 0, 0 }}; }
template <> inline std::string defaultStopsValue() { return {}; }
template <> inline TranslateAnchorType defaultStopsValue() { return {}; };
template <> inline RotateAnchorType defaultStopsValue() { return {}; };
template <> inline LineCapType defaultStopsValue() { return {}; };
template <> inline LineJoinType defaultStopsValue() { return {}; };
template <> inline SymbolPlacementType defaultStopsValue() { return {}; };
template <> inline TextAnchorType defaultStopsValue() { return {}; };
template <> inline TextJustifyType defaultStopsValue() { return {}; };
template <> inline TextTransformType defaultStopsValue() { return {}; };
template <> inline RotationAlignmentType defaultStopsValue() { return {}; };
template <typename T>
T NormalFunctionEvaluator<T>::operator()(const Function<T>& fn, const StyleCalculationParameters& parameters) const {
float base = fn.getBase();
const std::vector<std::pair<float, T>>& stops = fn.getStops();
float z = parameters.z;
bool smaller = false;
float smaller_z = 0.0f;
T smaller_val = T();
bool larger = false;
float larger_z = 0.0f;
T larger_val = T();
for (uint32_t i = 0; i < stops.size(); i++) {
float stop_z = stops[i].first;
T stop_val = stops[i].second;
if (stop_z <= z && (!smaller || smaller_z < stop_z)) {
smaller = true;
smaller_z = stop_z;
smaller_val = stop_val;
}
if (stop_z >= z && (!larger || larger_z > stop_z)) {
larger = true;
larger_z = stop_z;
larger_val = stop_val;
}
}
if (smaller && larger) {
if (larger_z == smaller_z || larger_val == smaller_val) {
return smaller_val;
}
const float zoomDiff = larger_z - smaller_z;
const float zoomProgress = z - smaller_z;
if (base == 1.0f) {
const float t = zoomProgress / zoomDiff;
return util::interpolate(smaller_val, larger_val, t);
} else {
const float t = (std::pow(base, zoomProgress) - 1) / (std::pow(base, zoomDiff) - 1);
return util::interpolate(smaller_val, larger_val, t);
}
} else if (larger) {
return larger_val;
} else if (smaller) {
return smaller_val;
} else {
// No stop defined.
return defaultStopsValue<T>();
}
}
template class NormalFunctionEvaluator<bool>;
template class NormalFunctionEvaluator<float>;
template class NormalFunctionEvaluator<Color>;
template class NormalFunctionEvaluator<std::vector<float>>;
template class NormalFunctionEvaluator<std::vector<std::string>>;
template class NormalFunctionEvaluator<std::array<float, 2>>;
template class NormalFunctionEvaluator<std::string>;
template class NormalFunctionEvaluator<TranslateAnchorType>;
template class NormalFunctionEvaluator<RotateAnchorType>;
template class NormalFunctionEvaluator<LineCapType>;
template class NormalFunctionEvaluator<LineJoinType>;
template class NormalFunctionEvaluator<SymbolPlacementType>;
template class NormalFunctionEvaluator<TextAnchorType>;
template class NormalFunctionEvaluator<TextJustifyType>;
template class NormalFunctionEvaluator<TextTransformType>;
template class NormalFunctionEvaluator<RotationAlignmentType>;
template <typename T>
inline size_t getBiggestStopLessThan(const std::vector<std::pair<float, T>>& stops, float z) {
for (uint32_t i = 0; i < stops.size(); i++) {
if (stops[i].first > z) {
return i == 0 ? i : i - 1;
}
}
return stops.size() - 1;
}
template <typename T>
Faded<T> CrossFadedFunctionEvaluator<T>::operator()(const Function<T>& fn, const StyleCalculationParameters& parameters) const {
Faded<T> result;
const std::vector<std::pair<float, T>>& stops = fn.getStops();
float z = parameters.z;
const float fraction = z - std::floor(z);
std::chrono::duration<float> d = parameters.defaultFadeDuration;
float t = std::min((parameters.now - parameters.zoomHistory.lastIntegerZoomTime) / d, 1.0f);
float fromScale = 1.0f;
float toScale = 1.0f;
size_t from, to;
if (z > parameters.zoomHistory.lastIntegerZoom) {
result.t = fraction + (1.0f - fraction) * t;
from = getBiggestStopLessThan(stops, z - 1.0f);
to = getBiggestStopLessThan(stops, z);
fromScale *= 2.0f;
} else {
result.t = 1 - (1 - t) * fraction;
to = getBiggestStopLessThan(stops, z);
from = getBiggestStopLessThan(stops, z + 1.0f);
fromScale /= 2.0f;
}
result.from = stops[from].second;
result.to = stops[to].second;
result.fromScale = fromScale;
result.toScale = toScale;
return result;
}
template class CrossFadedFunctionEvaluator<std::string>;
template class CrossFadedFunctionEvaluator<std::vector<float>>;
} // namespace mbgl