diff options
Diffstat (limited to 'include/mbgl/util')
-rw-r--r-- | include/mbgl/util/color.hpp | 40 | ||||
-rw-r--r-- | include/mbgl/util/constants.hpp | 7 | ||||
-rw-r--r-- | include/mbgl/util/convert.hpp | 17 | ||||
-rw-r--r-- | include/mbgl/util/enum.hpp | 60 | ||||
-rw-r--r-- | include/mbgl/util/exception.hpp | 16 | ||||
-rw-r--r-- | include/mbgl/util/font_stack.hpp | 17 | ||||
-rw-r--r-- | include/mbgl/util/geo.hpp | 24 | ||||
-rw-r--r-- | include/mbgl/util/geojson.hpp | 22 | ||||
-rw-r--r-- | include/mbgl/util/geometry.hpp | 44 | ||||
-rw-r--r-- | include/mbgl/util/image.hpp | 16 | ||||
-rw-r--r-- | include/mbgl/util/projection.hpp | 14 | ||||
-rw-r--r-- | include/mbgl/util/range.hpp | 25 | ||||
-rw-r--r-- | include/mbgl/util/tileset.hpp | 20 | ||||
-rw-r--r-- | include/mbgl/util/traits.hpp | 4 | ||||
-rw-r--r-- | include/mbgl/util/unitbezier.hpp | 39 | ||||
-rw-r--r-- | include/mbgl/util/variant.hpp | 10 |
16 files changed, 288 insertions, 87 deletions
diff --git a/include/mbgl/util/color.hpp b/include/mbgl/util/color.hpp new file mode 100644 index 0000000000..4be380fde3 --- /dev/null +++ b/include/mbgl/util/color.hpp @@ -0,0 +1,40 @@ +#pragma once + +#include <mbgl/util/optional.hpp> + +#include <string> + +namespace mbgl { + +// Stores a premultiplied color, with all four channels ranging from 0..1 +class Color { +public: + float r = 0.0f; + float g = 0.0f; + float b = 0.0f; + float a = 0.0f; + + static constexpr Color black() { return { 0.0f, 0.0f, 0.0f, 1.0f }; }; + static constexpr Color white() { return { 1.0f, 1.0f, 1.0f, 1.0f }; }; + + static optional<Color> parse(const std::string&); +}; + +constexpr bool operator==(const Color& colorA, const Color& colorB) { + return colorA.r == colorB.r && colorA.g == colorB.g && colorA.b == colorB.b && colorA.a == colorB.a; +} + +constexpr bool operator!=(const Color& colorA, const Color& colorB) { + return !(colorA == colorB); +} + +constexpr Color operator*(const Color& color, float alpha) { + return { + color.r * alpha, + color.g * alpha, + color.b * alpha, + color.a * alpha + }; +} + +} // namespace mbgl diff --git a/include/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp index 621fc1820e..3c0b3eb93e 100644 --- a/include/mbgl/util/constants.hpp +++ b/include/mbgl/util/constants.hpp @@ -1,6 +1,7 @@ #pragma once #include <mbgl/util/chrono.hpp> +#include <mbgl/util/unitbezier.hpp> #include <cmath> #include <string> @@ -17,7 +18,7 @@ constexpr float tileSize = 512; * In practice, all features are converted to this extent before being added. * * Positions are stored as signed 16bit integers. - * One bit is lost for signedness to support featuers extending past the left edge of the tile. + * One bit is lost for signedness to support features extending past the left edge of the tile. * One bit is lost because the line vertex buffer packs 1 bit of other data into the int. * One bit is lost to support features extending past the extent on the right edge of the tile. * This leaves us with 2^13 = 8192 @@ -35,11 +36,13 @@ constexpr double PITCH_MAX = M_PI / 3; constexpr double MIN_ZOOM = 0.0; constexpr double MAX_ZOOM = 25.5; -constexpr uint64_t DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024;; +constexpr uint64_t DEFAULT_MAX_CACHE_SIZE = 50 * 1024 * 1024; constexpr Duration DEFAULT_FADE_DURATION = Milliseconds(300); constexpr Seconds CLOCK_SKEW_RETRY_TIMEOUT { 30 }; +constexpr UnitBezier DEFAULT_TRANSITION_EASE = { 0, 0, 0.25, 1 }; + } // namespace util namespace debug { diff --git a/include/mbgl/util/convert.hpp b/include/mbgl/util/convert.hpp new file mode 100644 index 0000000000..c2b3d9950d --- /dev/null +++ b/include/mbgl/util/convert.hpp @@ -0,0 +1,17 @@ +#include <array> +#include <type_traits> +#include <utility> + +namespace mbgl { +namespace util { + +template<typename To, typename From, std::size_t Size, + typename = std::enable_if_t<std::is_convertible<From, To>::value>> +constexpr std::array<To, Size> convert(const std::array<From, Size>&from) { + std::array<To, Size> to {}; + std::copy(std::begin(from), std::end(from), std::begin(to)); + return to; +} + +} // namespace util +} // namespace mbgl diff --git a/include/mbgl/util/enum.hpp b/include/mbgl/util/enum.hpp index 3fbf313aed..48ffda463e 100644 --- a/include/mbgl/util/enum.hpp +++ b/include/mbgl/util/enum.hpp @@ -1,52 +1,36 @@ #pragma once -#include <iosfwd> +#include <mbgl/util/optional.hpp> + +#include <algorithm> +#include <cassert> #include <string> namespace mbgl { -template <typename Type> -struct EnumValue { - const Type value; - const char *name; -}; - -template <typename EnumName, const EnumValue<EnumName> *names, const size_t length> -struct Enum { - using Type = EnumName; - Type value; - static const constexpr size_t l = length; -private: - static constexpr inline bool compare(const char *a, const char *b) { - return *a == *b && (*a == '\0' || compare(a + 1, b + 1)); - } - static constexpr inline const char *lookup_type(Type e, EnumValue<Type> const * const list, size_t r) { - return r == 0 ? "" : list->value == e ? list->name : lookup_type(e, list + 1, r - 1); - } - static constexpr inline Type lookup_name(const char *n, EnumValue<Type> const * const list, size_t r) { - return r == 0 ? Type(-1) : compare(list->name, n) ? list->value : lookup_name(n, list + 1, r - 1); - } +template <typename T> +class Enum { public: - inline constexpr Enum(const char *n) : value(lookup_name(n, names, length)) {} - inline constexpr Enum(const std::string &n) : value(lookup_name(n.c_str(), names, length)) {} - inline constexpr Enum(Type t) : value(t) {} - - inline void operator=(const char *n) { value = lookup_name(n, names, length); } - inline void operator=(const std::string &n) { *this = n.c_str(); } - inline void operator=(Type t) { value = t; } + using Value = std::pair<const T, const char *>; - inline constexpr bool valid() const { return value != Type(-1); } + static const char * toString(T t) { + auto it = std::find_if(begin, end, [&] (const auto& v) { return t == v.first; }); + assert(it != end); return it->second; + } - inline constexpr const char *c_str() const { return lookup_type(value, names, length); } - inline std::string str() const { return c_str(); } + static optional<T> toEnum(const std::string& s) { + auto it = std::find_if(begin, end, [&] (const auto& v) { return s == v.second; }); + return it == end ? optional<T>() : it->first; + } - inline constexpr operator Type() const { return value; } +private: + static const Value* begin; + static const Value* end; }; -#define MBGL_DEFINE_ENUM_CLASS(name, type, strings...) \ - const constexpr ::mbgl::EnumValue<type> type##_names[] = strings; \ - using name = ::mbgl::Enum<type, type##_names, sizeof(type##_names) / sizeof(::mbgl::EnumValue<type>)>; \ - inline std::ostream& operator<<(std::ostream& os, type t) { return os << name(t).str(); } +#define MBGL_DEFINE_ENUM(type, strings...) \ +const constexpr Enum<type>::Value type##_names[] = strings; \ +template <> const Enum<type>::Value* Enum<type>::begin = std::begin(type##_names); \ +template <> const Enum<type>::Value* Enum<type>::end = std::end(type##_names) } // namespace mbgl - diff --git a/include/mbgl/util/exception.hpp b/include/mbgl/util/exception.hpp index b73a94fcd2..7c331636d4 100644 --- a/include/mbgl/util/exception.hpp +++ b/include/mbgl/util/exception.hpp @@ -6,23 +6,23 @@ namespace mbgl { namespace util { struct Exception : std::runtime_error { - inline Exception(const char *msg) : std::runtime_error(msg) {} - inline Exception(const std::string &msg) : std::runtime_error(msg) {} + Exception(const char *msg) : std::runtime_error(msg) {} + Exception(const std::string &msg) : std::runtime_error(msg) {} }; struct SpriteImageException : Exception { - inline SpriteImageException(const char *msg) : Exception(msg) {} - inline SpriteImageException(const std::string &msg) : Exception(msg) {} + SpriteImageException(const char *msg) : Exception(msg) {} + SpriteImageException(const std::string &msg) : Exception(msg) {} }; struct MisuseException : Exception { - inline MisuseException(const char *msg) : Exception(msg) {} - inline MisuseException(const std::string &msg) : Exception(msg) {} + MisuseException(const char *msg) : Exception(msg) {} + MisuseException(const std::string &msg) : Exception(msg) {} }; struct ShaderException : Exception { - inline ShaderException(const char *msg) : Exception(msg) {} - inline ShaderException(const std::string &msg) : Exception(msg) {} + ShaderException(const char *msg) : Exception(msg) {} + ShaderException(const std::string &msg) : Exception(msg) {} }; } // namespace util diff --git a/include/mbgl/util/font_stack.hpp b/include/mbgl/util/font_stack.hpp new file mode 100644 index 0000000000..d0b431e9ea --- /dev/null +++ b/include/mbgl/util/font_stack.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include <string> +#include <vector> + +namespace mbgl { + +// An array of font names +using FontStack = std::vector<std::string>; + +std::string fontStackToString(const FontStack&); + +struct FontStackHash { + std::size_t operator()(const FontStack&) const; +}; + +} // namespace mbgl diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 7e15d5c2f1..a68058048d 100644 --- a/include/mbgl/util/geo.hpp +++ b/include/mbgl/util/geo.hpp @@ -57,18 +57,18 @@ public: LatLng(const UnwrappedTileID& id); }; -inline bool operator==(const LatLng& a, const LatLng& b) { +constexpr bool operator==(const LatLng& a, const LatLng& b) { return a.latitude == b.latitude && a.longitude == b.longitude; } -inline bool operator!=(const LatLng& a, const LatLng& b) { +constexpr bool operator!=(const LatLng& a, const LatLng& b) { return !(a == b); } class ProjectedMeters { public: - double northing = 0; - double easting = 0; + double northing; + double easting; ProjectedMeters(double n = 0, double e = 0) : northing(n), easting(e) {} @@ -78,6 +78,10 @@ public: } }; +constexpr bool operator==(const ProjectedMeters& a, const ProjectedMeters& b) { + return a.northing == b.northing && a.easting == b.easting; +} + class LatLngBounds { public: // Return a bounds covering the entire (unwrapped) world. @@ -157,18 +161,18 @@ private: LatLng sw; LatLng ne; - LatLngBounds(const LatLng& sw_, const LatLng& ne_) - : sw(sw_), ne(ne_) {} + LatLngBounds(LatLng sw_, LatLng ne_) + : sw(std::move(sw_)), ne(std::move(ne_)) {} - friend bool operator==(const LatLngBounds&, const LatLngBounds&); - friend bool operator!=(const LatLngBounds&, const LatLngBounds&); + friend constexpr bool operator==(const LatLngBounds&, const LatLngBounds&); + friend constexpr bool operator!=(const LatLngBounds&, const LatLngBounds&); }; -inline bool operator==(const LatLngBounds& a, const LatLngBounds& b) { +constexpr bool operator==(const LatLngBounds& a, const LatLngBounds& b) { return a.sw == b.sw && a.ne == b.ne; } -inline bool operator!=(const LatLngBounds& a, const LatLngBounds& b) { +constexpr bool operator!=(const LatLngBounds& a, const LatLngBounds& b) { return !(a == b); } diff --git a/include/mbgl/util/geojson.hpp b/include/mbgl/util/geojson.hpp new file mode 100644 index 0000000000..3fd8c6ac4b --- /dev/null +++ b/include/mbgl/util/geojson.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include <memory> + +namespace mapbox { +namespace geojsonvt { +class GeoJSONVT; +} // namespace geojsonvt +} // namespace mapbox + +namespace mbgl { + +class GeoJSON { +public: + GeoJSON(std::unique_ptr<mapbox::geojsonvt::GeoJSONVT>); + GeoJSON(GeoJSON&&); + ~GeoJSON(); + + std::unique_ptr<mapbox::geojsonvt::GeoJSONVT> impl; +}; + +} // namespace mbgl diff --git a/include/mbgl/util/geometry.hpp b/include/mbgl/util/geometry.hpp new file mode 100644 index 0000000000..7fc2668c2c --- /dev/null +++ b/include/mbgl/util/geometry.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include <mapbox/geometry/geometry.hpp> +#include <mapbox/geometry/point_arithmetic.hpp> + +namespace mbgl { + +enum class FeatureType : uint8_t { + Unknown = 0, + Point = 1, + LineString = 2, + Polygon = 3 +}; + +template <class T> +using Point = mapbox::geometry::point<T>; + +template <class T> +using LineString = mapbox::geometry::line_string<T>; + +template <class T> +using Polygon = mapbox::geometry::polygon<T>; + +template <class T> +using MultiPoint = mapbox::geometry::multi_point<T>; + +template <class T> +using MultiLineString = mapbox::geometry::multi_line_string<T>; + +template <class T> +using MultiPolygon = mapbox::geometry::multi_polygon<T>; + +template <class T> +using LinearRing = mapbox::geometry::linear_ring<T>; + +template <class T> +using Geometry = mapbox::geometry::geometry<T>; + +template <class S, class T> +Point<S> convertPoint(const Point<T>& p) { + return Point<S>(p.x, p.y); +} + +} // namespace mbgl diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp index fbb0686a7b..124cdca7cd 100644 --- a/include/mbgl/util/image.hpp +++ b/include/mbgl/util/image.hpp @@ -1,5 +1,7 @@ #pragma once +#include <mbgl/util/noncopyable.hpp> + #include <string> #include <memory> #include <algorithm> @@ -12,7 +14,7 @@ enum ImageAlphaMode { }; template <ImageAlphaMode Mode> -class Image { +class Image : private util::noncopyable { public: Image() = default; @@ -26,6 +28,18 @@ public: height(h), data(std::move(data_)) {} + Image(Image&& o) + : width(o.width), + height(o.height), + data(std::move(o.data)) {} + + Image& operator=(Image&& o) { + width = o.width; + height = o.height; + data = std::move(o.data); + return *this; + } + bool operator==(const Image& rhs) const { return width == rhs.width && height == rhs.height && std::equal(data.get(), data.get() + size(), rhs.data.get(), diff --git a/include/mbgl/util/projection.hpp b/include/mbgl/util/projection.hpp index 8e1c994657..eb45088580 100644 --- a/include/mbgl/util/projection.hpp +++ b/include/mbgl/util/projection.hpp @@ -11,30 +11,32 @@ namespace mbgl { class Projection { public: - static inline double getMetersPerPixelAtLatitude(double lat, double zoom) { - const double mapPixelWidthAtZoom = std::pow(2.0, zoom) * util::tileSize; + static double getMetersPerPixelAtLatitude(double lat, double zoom) { + const double constrainedZoom = util::clamp(zoom, util::MIN_ZOOM, util::MAX_ZOOM); + const double mapPixelWidthAtZoom = std::pow(2.0, constrainedZoom) * util::tileSize; const double constrainedLatitude = util::clamp(lat, -util::LATITUDE_MAX, util::LATITUDE_MAX); - return std::cos(constrainedLatitude * util::DEG2RAD) * util::M2PI * util::EARTH_RADIUS_M / mapPixelWidthAtZoom; } - static inline ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) { + static ProjectedMeters projectedMetersForLatLng(const LatLng& latLng) { const double constrainedLatitude = util::clamp(latLng.latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX); + const double constrainedLongitude = util::clamp(latLng.longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); const double m = 1 - 1e-15; const double f = util::clamp(std::sin(util::DEG2RAD * constrainedLatitude), -m, m); - const double easting = util::EARTH_RADIUS_M * latLng.longitude * util::DEG2RAD; + const double easting = util::EARTH_RADIUS_M * constrainedLongitude * util::DEG2RAD; const double northing = 0.5 * util::EARTH_RADIUS_M * std::log((1 + f) / (1 - f)); return ProjectedMeters(northing, easting); } - static inline LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) { + static LatLng latLngForProjectedMeters(const ProjectedMeters& projectedMeters) { double latitude = (2 * std::atan(std::exp(projectedMeters.northing / util::EARTH_RADIUS_M)) - (M_PI / 2)) * util::RAD2DEG; double longitude = projectedMeters.easting * util::RAD2DEG / util::EARTH_RADIUS_M; latitude = util::clamp(latitude, -util::LATITUDE_MAX, util::LATITUDE_MAX); + longitude = util::clamp(longitude, -util::LONGITUDE_MAX, util::LONGITUDE_MAX); return LatLng(latitude, longitude); } diff --git a/include/mbgl/util/range.hpp b/include/mbgl/util/range.hpp new file mode 100644 index 0000000000..8da2dd45bb --- /dev/null +++ b/include/mbgl/util/range.hpp @@ -0,0 +1,25 @@ +#pragma once + +namespace mbgl { + +template <class T> +class Range { +public: + Range(const T& min_, const T& max_) + : min(min_), max(max_) {} + + T min; + T max; +}; + +template <class T> +bool operator==(const Range<T>& a, const Range<T>& b) { + return a.min == b.min && a.max == b.max; +} + +template <class T> +bool operator!=(const Range<T>& a, const Range<T>& b) { + return !(a == b); +} + +} // namespace mbgl diff --git a/include/mbgl/util/tileset.hpp b/include/mbgl/util/tileset.hpp new file mode 100644 index 0000000000..8a7fbe9b73 --- /dev/null +++ b/include/mbgl/util/tileset.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include <mbgl/util/range.hpp> + +#include <vector> +#include <string> +#include <cstdint> + +namespace mbgl { + +class Tileset { +public: + std::vector<std::string> tiles; + Range<uint8_t> zoomRange { 0, 22 }; + std::string attribution; + + // TileJSON also includes center, zoom, and bounds, but they are not used by mbgl. +}; + +} // namespace mbgl diff --git a/include/mbgl/util/traits.hpp b/include/mbgl/util/traits.hpp index 7c9499897e..9d6f947cd2 100644 --- a/include/mbgl/util/traits.hpp +++ b/include/mbgl/util/traits.hpp @@ -5,8 +5,8 @@ namespace mbgl { template<typename T> -constexpr auto underlying_type(T t) -> typename std::underlying_type<T>::type { - return static_cast<typename std::underlying_type<T>::type>(t); +constexpr auto underlying_type(T t) -> typename std::underlying_type_t<T> { + return typename std::underlying_type_t<T>(t); } } // namespace mbgl diff --git a/include/mbgl/util/unitbezier.hpp b/include/mbgl/util/unitbezier.hpp index ce3e78f3cc..3a4994917b 100644 --- a/include/mbgl/util/unitbezier.hpp +++ b/include/mbgl/util/unitbezier.hpp @@ -31,32 +31,31 @@ namespace mbgl { namespace util { struct UnitBezier { - UnitBezier(double p1x, double p1y, double p2x, double p2y) { - // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1). - cx = 3.0 * p1x; - bx = 3.0 * (p2x - p1x) - cx; - ax = 1.0 - cx - bx; - - cy = 3.0 * p1y; - by = 3.0 * (p2y - p1y) - cy; - ay = 1.0 - cy - by; + // Calculate the polynomial coefficients, implicit first and last control points are (0,0) and (1,1). + constexpr UnitBezier(double p1x, double p1y, double p2x, double p2y) + : cx(3.0 * p1x) + , bx(3.0 * (p2x - p1x) - cx) + , ax(1.0 - cx - bx) + , cy(3.0 * p1y) + , by(3.0 * (p2y - p1y) - cy) + , ay(1.0 - cy - by) { } - double sampleCurveX(double t) { + double sampleCurveX(double t) const { // `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. return ((ax * t + bx) * t + cx) * t; } - double sampleCurveY(double t) { + double sampleCurveY(double t) const { return ((ay * t + by) * t + cy) * t; } - double sampleCurveDerivativeX(double t) { + double sampleCurveDerivativeX(double t) const { return (3.0 * ax * t + 2.0 * bx) * t + cx; } // Given an x value, find a parametric value it came from. - double solveCurveX(double x, double epsilon) { + double solveCurveX(double x, double epsilon) const { double t0; double t1; double t2; @@ -100,18 +99,18 @@ struct UnitBezier { return t2; } - double solve(double x, double epsilon) { + double solve(double x, double epsilon) const { return sampleCurveY(solveCurveX(x, epsilon)); } private: - double ax; - double bx; - double cx; + const double cx; + const double bx; + const double ax; - double ay; - double by; - double cy; + const double cy; + const double by; + const double ay; }; } // namespace util diff --git a/include/mbgl/util/variant.hpp b/include/mbgl/util/variant.hpp new file mode 100644 index 0000000000..68a1208a17 --- /dev/null +++ b/include/mbgl/util/variant.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include <mapbox/variant.hpp> + +namespace mbgl { + +template <typename... T> +using variant = mapbox::util::variant<T...>; + +} // namespace mbgl |