diff options
Diffstat (limited to 'include/mbgl/util')
-rw-r--r-- | include/mbgl/util/char_array_buffer.hpp | 55 | ||||
-rw-r--r-- | include/mbgl/util/color.hpp | 34 | ||||
-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/feature.hpp | 6 | ||||
-rw-r--r-- | include/mbgl/util/geo.hpp | 32 | ||||
-rw-r--r-- | include/mbgl/util/geojson.hpp | 22 | ||||
-rw-r--r-- | include/mbgl/util/geometry.hpp | 24 | ||||
-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 |
15 files changed, 280 insertions, 95 deletions
diff --git a/include/mbgl/util/char_array_buffer.hpp b/include/mbgl/util/char_array_buffer.hpp new file mode 100644 index 0000000000..177f005477 --- /dev/null +++ b/include/mbgl/util/char_array_buffer.hpp @@ -0,0 +1,55 @@ +#pragma once + +#include <streambuf> + +namespace mbgl { +namespace util { + +// ref https://artofcode.wordpress.com/2010/12/12/deriving-from-stdstreambuf/ + +class CharArrayBuffer : public std::streambuf +{ +public: + CharArrayBuffer(char const* data, std::size_t size) + : begin_(data), end_(data + size), current_(data) {} + +private: + int_type underflow() final { + if (current_ == end_) { + return traits_type::eof(); + } + return traits_type::to_int_type(*current_); + } + + int_type uflow() final { + if (current_ == end_) { + return traits_type::eof(); + } + return traits_type::to_int_type(*current_++); + } + + int_type pbackfail(int_type ch) final { + if (current_ == begin_ || (ch != traits_type::eof() && ch != current_[-1])) { + return traits_type::eof(); + } + return traits_type::to_int_type(*--current_); + } + + std::streamsize showmanyc() final { + return end_ - current_; + } + + pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode) final { + if (dir == std::ios_base::beg) current_ = std::min(begin_ + off, end_); + else if (dir == std::ios_base::cur) current_ = std::min(current_ + off, end_); + else current_ = std::max(end_ - off, begin_); // dir == std::ios_base::end + return pos_type(off_type(current_ - begin_)); + } + + char const * const begin_; + char const * const end_; + char const * current_; +}; + +} // namespace util +} // namespace mbgl diff --git a/include/mbgl/util/color.hpp b/include/mbgl/util/color.hpp index d7fe61c640..4be380fde3 100644 --- a/include/mbgl/util/color.hpp +++ b/include/mbgl/util/color.hpp @@ -1,10 +1,40 @@ #pragma once -#include <array> +#include <mbgl/util/optional.hpp> + +#include <string> namespace mbgl { // Stores a premultiplied color, with all four channels ranging from 0..1 -using Color = std::array<float, 4>; +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/feature.hpp b/include/mbgl/util/feature.hpp index 7747d34ee9..7c5c8d7625 100644 --- a/include/mbgl/util/feature.hpp +++ b/include/mbgl/util/feature.hpp @@ -7,13 +7,13 @@ namespace mbgl { using Value = mapbox::geometry::value; - +using NullValue = mapbox::geometry::null_value_t; +using PropertyMap = mapbox::geometry::property_map; +using FeatureIdentifier = mapbox::geometry::identifier; class Feature : public mapbox::geometry::feature<double> { public: Feature(geometry_type&& geometry_) : mapbox::geometry::feature<double> { std::move(geometry_) } {} - - optional<uint64_t> id; }; } // namespace mbgl diff --git a/include/mbgl/util/geo.hpp b/include/mbgl/util/geo.hpp index 7e15d5c2f1..9dca10eb84 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); } @@ -187,12 +191,12 @@ public: double left = 0; ///< Number of pixels inset from the left edge. double bottom = 0; ///< Number of pixels inset from the bottom edge. double right = 0; ///< Number of pixels inset from the right edge. - + EdgeInsets() {} - + EdgeInsets(const double t, const double l, const double b, const double r) : top(t), left(l), bottom(b), right(r) {} - + explicit operator bool() const { return !(std::isnan(top) || std::isnan(left) || std::isnan(bottom) || std::isnan(right)) && (top || left || bottom || right); @@ -210,7 +214,7 @@ public: top + o.top, left + o.left, bottom + o.bottom, right + o.right, }; } - + ScreenCoordinate getCenter(uint16_t width, uint16_t height) const; }; 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 index 6b9c332bf2..6dc16bc514 100644 --- a/include/mbgl/util/geometry.hpp +++ b/include/mbgl/util/geometry.hpp @@ -5,6 +5,13 @@ namespace mbgl { +enum class FeatureType : uint8_t { + Unknown = 0, + Point = 1, + LineString = 2, + Polygon = 3 +}; + template <class T> using Point = mapbox::geometry::point<T>; @@ -34,4 +41,21 @@ Point<S> convertPoint(const Point<T>& p) { return Point<S>(p.x, p.y); } +struct ToFeatureType { + template <class T> + FeatureType operator()(const Point<T> &) const { return FeatureType::Point; } + template <class T> + FeatureType operator()(const MultiPoint<T> &) const { return FeatureType::Point; } + template <class T> + FeatureType operator()(const LineString<T> &) const { return FeatureType::LineString; } + template <class T> + FeatureType operator()(const MultiLineString<T> &) const { return FeatureType::LineString; } + template <class T> + FeatureType operator()(const Polygon<T> &) const { return FeatureType::Polygon; } + template <class T> + FeatureType operator()(const MultiPolygon<T> &) const { return FeatureType::Polygon; } + template <class T> + FeatureType operator()(const mapbox::geometry::geometry_collection<T> &) const { return FeatureType::Unknown; } +}; + } // namespace mbgl 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 |