diff --git a/include/mbgl/map/annotation.hpp b/include/mbgl/map/annotation.hpp index e88d98b5c..2eb0509d9 100644 --- a/include/mbgl/map/annotation.hpp +++ b/include/mbgl/map/annotation.hpp @@ -2,7 +2,6 @@ #define MBGL_MAP_ANNOTATIONS #include -#include #include #include #include @@ -18,55 +17,38 @@ namespace mbgl { class Annotation; class Map; +class LiveTile; -typedef std::vector AnnotationSegment; - -enum class AnnotationType : uint8_t { - Point, - Shape -}; +using AnnotationIDs = std::vector; class AnnotationManager : private util::noncopyable { public: AnnotationManager(); + ~AnnotationManager(); - void setDefaultPointAnnotationSymbol(std::string& symbol) { defaultPointAnnotationSymbol = symbol; } - std::pair, std::vector> addPointAnnotations(std::vector, std::vector& symbols, const Map&); - std::vector removeAnnotations(std::vector); - std::vector getAnnotationsInBounds(LatLngBounds, const Map&) const; - LatLngBounds getBoundsForAnnotations(std::vector) const; + void setDefaultPointAnnotationSymbol(const std::string& symbol); + std::pair, AnnotationIDs> addPointAnnotations( + const std::vector&, const std::vector& symbols, const Map&); + std::vector removeAnnotations(const AnnotationIDs&); + AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const Map&) const; + LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const; - const std::unique_ptr& getTile(Tile::ID const& id); + const LiveTile* getTile(Tile::ID const& id); + + static const std::string layerID; private: - uint32_t nextID() { return nextID_++; } - static vec2 projectPoint(LatLng& point); + inline uint32_t nextID(); + static vec2 projectPoint(const LatLng& point); private: - std::mutex mtx; + mutable std::mutex mtx; std::string defaultPointAnnotationSymbol; std::map> annotations; - std::map, std::unique_ptr>> annotationTiles; - std::unique_ptr nullTile; + std::map>> annotationTiles; uint32_t nextID_ = 0; }; -class Annotation : private util::noncopyable { - friend class AnnotationManager; -public: - Annotation(AnnotationType, std::vector); - -private: - LatLng getPoint() const; - LatLngBounds getBounds() const { return bounds; } - -private: - const AnnotationType type = AnnotationType::Point; - const std::vector geometry; - std::map>> tileFeatures; - LatLngBounds bounds; -}; - } #endif diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index e5eabdac4..0fb2065cc 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -152,13 +152,14 @@ public: inline const LatLng latLngForPixel(const vec2 pixel) const { return state.latLngForPixel(pixel); } // Annotations - void setDefaultPointAnnotationSymbol(std::string&); - uint32_t addPointAnnotation(LatLng, std::string& symbol); - std::vector addPointAnnotations(std::vector, std::vector& symbols); + void setDefaultPointAnnotationSymbol(const std::string&); + uint32_t addPointAnnotation(const LatLng&, const std::string& symbol); + std::vector addPointAnnotations(const std::vector&, + const std::vector& symbols); void removeAnnotation(uint32_t); - void removeAnnotations(std::vector); - std::vector getAnnotationsInBounds(LatLngBounds) const; - LatLngBounds getBoundsForAnnotations(std::vector) const; + void removeAnnotations(const std::vector&); + std::vector getAnnotationsInBounds(const LatLngBounds&) const; + LatLngBounds getBoundsForAnnotations(const std::vector&) const; // Debug void setDebug(bool value); @@ -198,7 +199,7 @@ private: // the stylesheet. void prepare(); - void updateAnnotationTiles(std::vector&); + void updateAnnotationTiles(const std::vector&); enum class Mode : uint8_t { None, // we're not doing any processing @@ -248,7 +249,7 @@ private: std::unique_ptr lineAtlas; util::ptr texturePool; std::unique_ptr painter; - util::ptr annotationManager; + std::unique_ptr annotationManager; const std::unique_ptr data; diff --git a/include/mbgl/util/constants.hpp b/include/mbgl/util/constants.hpp index 9e0856b68..e598806c2 100644 --- a/include/mbgl/util/constants.hpp +++ b/include/mbgl/util/constants.hpp @@ -16,8 +16,6 @@ extern const double M2PI; extern const double EARTH_RADIUS_M; extern const double LATITUDE_MAX; -extern const std::string ANNOTATIONS_POINTS_LAYER_ID; - } namespace debug { diff --git a/src/mbgl/map/annotation.cpp b/src/mbgl/map/annotation.cpp index 83739ab46..54b2ebf16 100644 --- a/src/mbgl/map/annotation.cpp +++ b/src/mbgl/map/annotation.cpp @@ -1,60 +1,120 @@ #include #include +#include #include #include #include #include -using namespace mbgl; +namespace mbgl { -Annotation::Annotation(AnnotationType type_, std::vector geometry_) +enum class AnnotationType : uint8_t { + Point, + Shape +}; + +using AnnotationSegment = std::vector; +using AnnotationSegments = std::vector; + +class Annotation : private util::noncopyable { + friend class AnnotationManager; +public: + Annotation(AnnotationType, const AnnotationSegments&); + +private: + LatLng getPoint() const; + LatLngBounds getBounds() const { return bounds; } + +private: + const AnnotationType type = AnnotationType::Point; + const AnnotationSegments geometry; + std::map>> tileFeatures; + const LatLngBounds bounds; +}; + +Annotation::Annotation(AnnotationType type_, const AnnotationSegments& geometry_) : type(type_), - geometry(geometry_) { - if (type == AnnotationType::Point) { - bounds = LatLngBounds(getPoint(), getPoint()); - } else { - for (auto segment : geometry) { - for (auto point : segment) { - bounds.extend(point); - } - } - } + geometry(geometry_), + bounds([this] { + LatLngBounds bounds_; + if (type == AnnotationType::Point) { + bounds_ = { getPoint(), getPoint() }; + } else { + for (auto& segment : geometry) { + for (auto& point : segment) { + bounds_.extend(point); + } + } + } + return bounds_; + }()) { } LatLng Annotation::getPoint() const { + // Return the first line's first point. Shortcut for point annotations. + assert(!geometry.empty()); + assert(!geometry[0].empty()); return geometry[0][0]; } -AnnotationManager::AnnotationManager() - : nullTile(util::make_unique()) {} +AnnotationManager::AnnotationManager() {} -vec2 AnnotationManager::projectPoint(LatLng& point) { - double sine = std::sin(point.latitude * M_PI / 180); - double x = point.longitude / 360 + 0.5; - double y = 0.5 - 0.25 * std::log((1 + sine) / (1 - sine)) / M_PI; - return vec2(x, y); +AnnotationManager::~AnnotationManager() { + // leave this here because the header file doesn't have a definition of + // Annotation so we can't destruct the object with just the header file. } -std::pair, std::vector> AnnotationManager::addPointAnnotations(std::vector points, std::vector& symbols, const Map& map) { +void AnnotationManager::setDefaultPointAnnotationSymbol(const std::string& symbol) { + std::lock_guard lock(mtx); + defaultPointAnnotationSymbol = symbol; +} - uint16_t extent = 4096; +uint32_t AnnotationManager::nextID() { + return nextID_++; +} + +vec2 AnnotationManager::projectPoint(const LatLng& point) { + // Project a coordinate into unit space in a square map. + const double sine = std::sin(point.latitude * M_PI / 180.0); + const double x = point.longitude / 360.0 + 0.5; + const double y = 0.5 - 0.25 * std::log((1.0 + sine) / (1.0 - sine)) / M_PI; + return { x, y }; +} + +std::pair, AnnotationIDs> AnnotationManager::addPointAnnotations( + const std::vector& points, const std::vector& symbols, const Map& map) { + std::lock_guard lock(mtx); + + // We pre-generate tiles to contain each annotation up to the map's max zoom. + // We do this for fast rendering without projection conversions on the fly, as well as + // to simplify bounding box queries of annotations later. Tiles get invalidated when + // annotations are added or removed in order to refresh the map render without + // touching the base map underneath. + + const uint16_t extent = 4096; std::vector annotationIDs; annotationIDs.reserve(points.size()); std::vector affectedTiles; - for (uint32_t i = 0; i < points.size(); ++i) { - uint32_t annotationID = nextID(); + for (size_t i = 0; i < points.size(); ++i) { + const uint32_t annotationID = nextID(); - auto anno_it = annotations.emplace(annotationID, util::make_unique(AnnotationType::Point, std::vector({{ points[i] }}))); + // track the annotation global ID and its geometry + auto anno_it = annotations.emplace( + annotationID, + util::make_unique(AnnotationType::Point, + AnnotationSegments({ { points[i] } }))); - uint8_t maxZoom = map.getMaxZoom(); + const uint8_t maxZoom = map.getMaxZoom(); + // side length of map at this zoom uint32_t z2 = 1 << maxZoom; - vec2 p = projectPoint(points[i]); + // projection conversion into unit space + const vec2 p = projectPoint(points[i]); uint32_t x = p.x * z2; uint32_t y = p.y * z2; @@ -63,36 +123,51 @@ std::pair, std::vector> AnnotationManager::addPo affectedTiles.emplace_back(z, x, y); Tile::ID tileID = affectedTiles.back(); - Coordinate coordinate(extent * (p.x * z2 - x), extent * (p.y * z2 - y)); + // calculate tile coordinate + const Coordinate coordinate(extent * (p.x * z2 - x), extent * (p.y * z2 - y)); - GeometryCollection geometries({{ {{ coordinate }} }}); + const GeometryCollection geometries({ { { { coordinate } } } }); - std::map properties = {{ "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) }}; + // at render time we style the annotation according to its {sprite} field + const std::map properties = { + { "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) } + }; - auto feature = std::make_shared(FeatureType::Point, - geometries, - properties); + auto feature = + std::make_shared(FeatureType::Point, geometries, properties); auto tile_it = annotationTiles.find(tileID); if (tile_it != annotationTiles.end()) { + // + // We have this tile created already. Add this feature to it. + // // get point layer & add feature - auto layer = tile_it->second.second->getMutableLayer(util::ANNOTATIONS_POINTS_LAYER_ID); + auto layer = + tile_it->second.second->getMutableLayer(layerID); layer->addFeature(feature); // record annotation association with tile tile_it->second.first.push_back(annotationID); } else { + // + // We need to create a new tile for this feature. + // // create point layer & add feature util::ptr layer = std::make_shared(); layer->addFeature(feature); // create tile & record annotation association - auto tile_pos = annotationTiles.emplace(tileID, std::make_pair(std::vector({ annotationID }), util::make_unique())); + auto tile_pos = annotationTiles.emplace( + tileID, std::make_pair(AnnotationIDs({ annotationID }), + util::make_unique())); // add point layer to tile - tile_pos.first->second.second->addLayer(util::ANNOTATIONS_POINTS_LAYER_ID, layer); + tile_pos.first->second.second->addLayer(layerID, layer); } - // record annotation association with tile feature - anno_it.first->second->tileFeatures.emplace(tileID, std::vector>({ feature })); + // Record annotation association with tile and tile feature. This is used to determine stale tiles, + // as well as to remove the feature from the tile upon annotation deletion. + anno_it.first->second->tileFeatures.emplace( + tileID, std::vector>({ feature })); + // get ready for the next-lower zoom number z2 /= 2; x /= 2; y /= 2; @@ -101,26 +176,32 @@ std::pair, std::vector> AnnotationManager::addPo annotationIDs.push_back(annotationID); } + // Tile:IDs that need refreshed and the annotation identifiers held onto by the client. return std::make_pair(affectedTiles, annotationIDs); } -std::vector AnnotationManager::removeAnnotations(std::vector ids) { +std::vector AnnotationManager::removeAnnotations(const AnnotationIDs& ids) { + std::lock_guard lock(mtx); + std::vector affectedTiles; for (auto& annotationID : ids) { - auto annotation_it = annotations.find(annotationID); + const auto annotation_it = annotations.find(annotationID); if (annotation_it != annotations.end()) { auto& annotation = annotation_it->second; for (auto& tile_it : annotationTiles) { + // clean up tile -> annotation mapping used for bounds queries auto& tileAnnotations = tile_it.second.first; - util::erase_if(tileAnnotations, tileAnnotations.begin(), - tileAnnotations.end(), [&](const uint32_t annotationID_) -> bool { - return (annotationID_ == annotationID); - }); + util::erase_if(tileAnnotations, tileAnnotations.begin(), tileAnnotations.end(), + [&](const uint32_t annotationID_) + -> bool { return (annotationID_ == annotationID); }); + // remove annotation feature from tile layer auto features_it = annotation->tileFeatures.find(tile_it.first); if (features_it != annotation->tileFeatures.end()) { - auto layer = tile_it.second.second->getMutableLayer(util::ANNOTATIONS_POINTS_LAYER_ID); - auto& features = features_it->second; + const auto layer = + tile_it.second.second->getMutableLayer(layerID); + const auto& features = features_it->second; + assert(!features.empty()); layer->removeFeature(features[0]); affectedTiles.push_back(tile_it.first); } @@ -129,18 +210,22 @@ std::vector AnnotationManager::removeAnnotations(std::vector } } + // Tile::IDs for tiles that need refreshed. return affectedTiles; } -std::vector AnnotationManager::getAnnotationsInBounds(LatLngBounds queryBounds, const Map& map) const { - uint8_t z = map.getMaxZoom(); - uint32_t z2 = 1 << z; - vec2 swPoint = projectPoint(queryBounds.sw); - vec2 nePoint = projectPoint(queryBounds.ne); +std::vector AnnotationManager::getAnnotationsInBounds(const LatLngBounds& queryBounds, + const Map& map) const { + std::lock_guard lock(mtx); + + const uint8_t z = map.getMaxZoom(); + const uint32_t z2 = 1 << z; + const vec2 swPoint = projectPoint(queryBounds.sw); + const vec2 nePoint = projectPoint(queryBounds.ne); // tiles number y from top down - Tile::ID nwTile(z, swPoint.x * z2, nePoint.y * z2); - Tile::ID seTile(z, nePoint.x * z2, swPoint.y * z2); + const Tile::ID nwTile(z, swPoint.x * z2, nePoint.y * z2); + const Tile::ID seTile(z, nePoint.x * z2, swPoint.y * z2); std::vector matchingAnnotations; @@ -149,17 +234,26 @@ std::vector AnnotationManager::getAnnotationsInBounds(LatLngBounds que if (id.z == z) { if (id.x >= nwTile.x && id.x <= seTile.x && id.y >= nwTile.y && id.y <= seTile.y) { if (id.x > nwTile.x && id.x < seTile.x && id.y > nwTile.y && id.y < seTile.y) { - // trivial accept; grab all of the tile's annotations - std::copy(tile.second.first.begin(), tile.second.first.end(), std::back_inserter(matchingAnnotations)); + // Trivial accept; this tile is completely inside the query bounds, so + // we'll return all of its annotations. + std::copy(tile.second.first.begin(), tile.second.first.end(), + std::back_inserter(matchingAnnotations)); } else { - // check tile's annotations' bounding boxes + // This tile is intersected by the query bounds. We need to check the + // tile's annotations' bounding boxes individually. std::copy_if(tile.second.first.begin(), tile.second.first.end(), - std::back_inserter(matchingAnnotations), [&](const uint32_t annotationID) -> bool { - LatLngBounds annoBounds = this->annotations.find(annotationID)->second->getBounds(); - return (annoBounds.sw.latitude >= queryBounds.sw.latitude && - annoBounds.ne.latitude <= queryBounds.ne.latitude && - annoBounds.sw.longitude >= queryBounds.sw.longitude && - annoBounds.ne.longitude <= queryBounds.ne.longitude); + std::back_inserter(matchingAnnotations), + [&](const uint32_t annotationID) -> bool { + const auto it = annotations.find(annotationID); + if (it != annotations.end()) { + const LatLngBounds annoBounds = it->second->getBounds(); + return (annoBounds.sw.latitude >= queryBounds.sw.latitude && + annoBounds.ne.latitude <= queryBounds.ne.latitude && + annoBounds.sw.longitude >= queryBounds.sw.longitude && + annoBounds.ne.longitude <= queryBounds.ne.longitude); + } else { + return false; + } }); } } @@ -169,10 +263,12 @@ std::vector AnnotationManager::getAnnotationsInBounds(LatLngBounds que return matchingAnnotations; } -LatLngBounds AnnotationManager::getBoundsForAnnotations(std::vector ids) const { +LatLngBounds AnnotationManager::getBoundsForAnnotations(const AnnotationIDs& ids) const { + std::lock_guard lock(mtx); + LatLngBounds bounds; for (auto id : ids) { - auto annotation_it = annotations.find(id); + const auto annotation_it = annotations.find(id); if (annotation_it != annotations.end()) { bounds.extend(annotation_it->second->getPoint()); } @@ -181,12 +277,16 @@ LatLngBounds AnnotationManager::getBoundsForAnnotations(std::vector id return bounds; } -const std::unique_ptr& AnnotationManager::getTile(Tile::ID const& id) { +const LiveTile* AnnotationManager::getTile(Tile::ID const& id) { std::lock_guard lock(mtx); - auto tile_it = annotationTiles.find(id); + const auto tile_it = annotationTiles.find(id); if (tile_it != annotationTiles.end()) { - return tile_it->second.second; + return tile_it->second.second.get(); } - return nullTile; + return nullptr; +} + +const std::string AnnotationManager::layerID = "com.mapbox.annotations.points"; + } diff --git a/src/mbgl/map/live_tile.hpp b/src/mbgl/map/live_tile.hpp index 6a24518d8..e4027c8c1 100644 --- a/src/mbgl/map/live_tile.hpp +++ b/src/mbgl/map/live_tile.hpp @@ -42,7 +42,6 @@ public: void addLayer(const std::string&, util::ptr); util::ptr getLayer(const std::string&) const override; util::ptr getMutableLayer(const std::string&) const; - bool operator()(const LiveTile&) const { return layers.size() > 0; } private: std::map> layers; diff --git a/src/mbgl/map/live_tile_data.cpp b/src/mbgl/map/live_tile_data.cpp index 192efb7dc..dd83ed422 100644 --- a/src/mbgl/map/live_tile_data.cpp +++ b/src/mbgl/map/live_tile_data.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -36,7 +37,7 @@ void LiveTileData::parse() { } if (source.type == SourceType::Annotations) { - const std::unique_ptr& tile = annotationManager.getTile(id); + const LiveTile* tile = annotationManager.getTile(id); if (tile) { // Parsing creates state that is encapsulated in TileParser. While parsing, diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index bc53d0d0f..5cc08ace6 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -540,19 +540,19 @@ std::string Map::getAccessToken() const { #pragma mark - Annotations -void Map::setDefaultPointAnnotationSymbol(std::string& symbol) { +void Map::setDefaultPointAnnotationSymbol(const std::string& symbol) { assert(Environment::currentlyOn(ThreadType::Main)); annotationManager->setDefaultPointAnnotationSymbol(symbol); } -uint32_t Map::addPointAnnotation(LatLng point, std::string& symbol) { +uint32_t Map::addPointAnnotation(const LatLng& point, const std::string& symbol) { assert(Environment::currentlyOn(ThreadType::Main)); std::vector points({ point }); std::vector symbols({ symbol }); return addPointAnnotations(points, symbols)[0]; } -std::vector Map::addPointAnnotations(std::vector points, std::vector& symbols) { +std::vector Map::addPointAnnotations(const std::vector& points, const std::vector& symbols) { assert(Environment::currentlyOn(ThreadType::Main)); auto result = annotationManager->addPointAnnotations(points, symbols, *this); updateAnnotationTiles(result.first); @@ -564,23 +564,24 @@ void Map::removeAnnotation(uint32_t annotation) { removeAnnotations({ annotation }); } -void Map::removeAnnotations(std::vector annotations) { +void Map::removeAnnotations(const std::vector& annotations) { assert(Environment::currentlyOn(ThreadType::Main)); auto result = annotationManager->removeAnnotations(annotations); updateAnnotationTiles(result); } -std::vector Map::getAnnotationsInBounds(LatLngBounds bounds) const { +std::vector Map::getAnnotationsInBounds(const LatLngBounds& bounds) const { assert(Environment::currentlyOn(ThreadType::Main)); return annotationManager->getAnnotationsInBounds(bounds, *this); } -LatLngBounds Map::getBoundsForAnnotations(std::vector annotations) const { +LatLngBounds Map::getBoundsForAnnotations(const std::vector& annotations) const { assert(Environment::currentlyOn(ThreadType::Main)); return annotationManager->getBoundsForAnnotations(annotations); } -void Map::updateAnnotationTiles(std::vector& ids) { +void Map::updateAnnotationTiles(const std::vector& ids) { + assert(Environment::currentlyOn(ThreadType::Main)); for (const auto &source : activeSources) { if (source->info.type == SourceType::Annotations) { source->source->invalidateTiles(*this, ids); diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index 3036342d7..818906888 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -373,7 +373,7 @@ void Source::update(Map &map, updated = map.getTime(); } -void Source::invalidateTiles(Map& map, std::vector& ids) { +void Source::invalidateTiles(Map& map, const std::vector& ids) { for (auto& id : ids) { tiles.erase(id); tile_data.erase(id); diff --git a/src/mbgl/map/source.hpp b/src/mbgl/map/source.hpp index f5ffd29ad..4cdd2ed64 100644 --- a/src/mbgl/map/source.hpp +++ b/src/mbgl/map/source.hpp @@ -37,7 +37,7 @@ public: void load(Map &, Environment &); void update(Map &, uv::worker &, util::ptr