make more things const and refs, and add mutexes

This commit is contained in:
Konstantin Käfer
2015-03-20 13:14:00 +01:00
parent 0cdf21c62d
commit f73dfc73f1
10 changed files with 179 additions and 122 deletions

View File

@@ -2,7 +2,6 @@
#define MBGL_MAP_ANNOTATIONS
#include <mbgl/map/tile.hpp>
#include <mbgl/map/live_tile.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/std.hpp>
@@ -18,55 +17,38 @@ namespace mbgl {
class Annotation;
class Map;
class LiveTile;
typedef std::vector<LatLng> AnnotationSegment;
enum class AnnotationType : uint8_t {
Point,
Shape
};
using AnnotationIDs = std::vector<uint32_t>;
class AnnotationManager : private util::noncopyable {
public:
AnnotationManager();
~AnnotationManager();
void setDefaultPointAnnotationSymbol(std::string& symbol) { defaultPointAnnotationSymbol = symbol; }
std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> addPointAnnotations(std::vector<LatLng>, std::vector<std::string>& symbols, const Map&);
std::vector<Tile::ID> removeAnnotations(std::vector<uint32_t>);
std::vector<uint32_t> getAnnotationsInBounds(LatLngBounds, const Map&) const;
LatLngBounds getBoundsForAnnotations(std::vector<uint32_t>) const;
void setDefaultPointAnnotationSymbol(const std::string& symbol);
std::pair<std::vector<Tile::ID>, AnnotationIDs> addPointAnnotations(
const std::vector<LatLng>&, const std::vector<std::string>& symbols, const Map&);
std::vector<Tile::ID> removeAnnotations(const AnnotationIDs&);
AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const Map&) const;
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const;
const std::unique_ptr<LiveTile>& 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<double> projectPoint(LatLng& point);
inline uint32_t nextID();
static vec2<double> projectPoint(const LatLng& point);
private:
std::mutex mtx;
mutable std::mutex mtx;
std::string defaultPointAnnotationSymbol;
std::map<uint32_t, std::unique_ptr<Annotation>> annotations;
std::map<Tile::ID, std::pair<std::vector<uint32_t>, std::unique_ptr<LiveTile>>> annotationTiles;
std::unique_ptr<LiveTile> nullTile;
std::map<Tile::ID, std::pair<AnnotationIDs, std::unique_ptr<LiveTile>>> annotationTiles;
uint32_t nextID_ = 0;
};
class Annotation : private util::noncopyable {
friend class AnnotationManager;
public:
Annotation(AnnotationType, std::vector<AnnotationSegment>);
private:
LatLng getPoint() const;
LatLngBounds getBounds() const { return bounds; }
private:
const AnnotationType type = AnnotationType::Point;
const std::vector<AnnotationSegment> geometry;
std::map<Tile::ID, std::vector<std::weak_ptr<const LiveTileFeature>>> tileFeatures;
LatLngBounds bounds;
};
}
#endif

View File

@@ -142,13 +142,14 @@ public:
inline const LatLng latLngForPixel(const vec2<double> pixel) const { return state.latLngForPixel(pixel); }
// Annotations
void setDefaultPointAnnotationSymbol(std::string&);
uint32_t addPointAnnotation(LatLng, std::string& symbol);
std::vector<uint32_t> addPointAnnotations(std::vector<LatLng>, std::vector<std::string>& symbols);
void setDefaultPointAnnotationSymbol(const std::string&);
uint32_t addPointAnnotation(const LatLng&, const std::string& symbol);
std::vector<uint32_t> addPointAnnotations(const std::vector<LatLng>&,
const std::vector<std::string>& symbols);
void removeAnnotation(uint32_t);
void removeAnnotations(std::vector<uint32_t>);
std::vector<uint32_t> getAnnotationsInBounds(LatLngBounds) const;
LatLngBounds getBoundsForAnnotations(std::vector<uint32_t>) const;
void removeAnnotations(const std::vector<uint32_t>&);
std::vector<uint32_t> getAnnotationsInBounds(const LatLngBounds&) const;
LatLngBounds getBoundsForAnnotations(const std::vector<uint32_t>&) const;
// Debug
void setDebug(bool value);
@@ -181,7 +182,7 @@ private:
// the stylesheet.
void prepare();
void updateAnnotationTiles(std::vector<Tile::ID>&);
void updateAnnotationTiles(const std::vector<Tile::ID>&);
enum class Mode : uint8_t {
None, // we're not doing any processing
@@ -230,7 +231,7 @@ private:
const std::unique_ptr<LineAtlas> lineAtlas;
util::ptr<TexturePool> texturePool;
const std::unique_ptr<Painter> painter;
util::ptr<AnnotationManager> annotationManager;
const std::unique_ptr<AnnotationManager> annotationManager;
std::string styleURL;
std::string styleJSON = "";

View File

@@ -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 {

View File

@@ -1,58 +1,108 @@
#include <mbgl/map/annotation.hpp>
#include <mbgl/map/map.hpp>
#include <mbgl/map/live_tile.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/std.hpp>
#include <algorithm>
#include <memory>
using namespace mbgl;
namespace mbgl {
Annotation::Annotation(AnnotationType type_, std::vector<AnnotationSegment> geometry_)
enum class AnnotationType : uint8_t {
Point,
Shape
};
using AnnotationSegment = std::vector<LatLng>;
using AnnotationSegments = std::vector<AnnotationSegment>;
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<Tile::ID, std::vector<std::weak_ptr<const LiveTileFeature>>> 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 {
assert(!geometry.empty());
assert(!geometry[0].empty());
return geometry[0][0];
}
AnnotationManager::AnnotationManager()
: nullTile(util::make_unique<LiveTile>()) {}
AnnotationManager::AnnotationManager() {}
vec2<double> 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<double>(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<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPointAnnotations(std::vector<LatLng> points, std::vector<std::string>& symbols, const Map& map) {
void AnnotationManager::setDefaultPointAnnotationSymbol(const std::string& symbol) {
std::lock_guard<std::mutex> lock(mtx);
defaultPointAnnotationSymbol = symbol;
}
uint16_t extent = 4096;
uint32_t AnnotationManager::nextID() {
return nextID_++;
}
std::vector<uint32_t> annotationIDs(points.size());
vec2<double> AnnotationManager::projectPoint(const LatLng& point) {
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<std::vector<Tile::ID>, AnnotationIDs> AnnotationManager::addPointAnnotations(
const std::vector<LatLng>& points, const std::vector<std::string>& symbols, const Map& map) {
std::lock_guard<std::mutex> lock(mtx);
const uint16_t extent = 4096;
AnnotationIDs annotationIDs(points.size());
std::vector<Tile::ID> 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<Annotation>(AnnotationType::Point, std::vector<AnnotationSegment>({{ points[i] }})));
auto anno_it = annotations.emplace(
annotationID,
util::make_unique<Annotation>(AnnotationType::Point,
AnnotationSegments({ { points[i] } })));
uint8_t maxZoom = map.getMaxZoom();
const uint8_t maxZoom = map.getMaxZoom();
uint32_t z2 = 1 << maxZoom;
vec2<double> p = projectPoint(points[i]);
const vec2<double> p = projectPoint(points[i]);
uint32_t x = p.x * z2;
uint32_t y = p.y * z2;
@@ -61,20 +111,22 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> 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));
const Coordinate coordinate(extent * (p.x * z2 - x), extent * (p.y * z2 - y));
GeometryCollection geometries({{ {{ coordinate }} }});
const GeometryCollection geometries({ { { { coordinate } } } });
std::map<std::string, std::string> properties = {{ "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) }};
const std::map<std::string, std::string> properties = {
{ "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) }
};
auto feature = std::make_shared<const LiveTileFeature>(FeatureType::Point,
geometries,
properties);
auto feature =
std::make_shared<const LiveTileFeature>(FeatureType::Point, geometries, properties);
auto tile_it = annotationTiles.find(tileID);
if (tile_it != annotationTiles.end()) {
// 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);
@@ -83,13 +135,16 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
util::ptr<LiveTileLayer> layer = std::make_shared<LiveTileLayer>();
layer->addFeature(feature);
// create tile & record annotation association
auto tile_pos = annotationTiles.emplace(tileID, std::make_pair(std::vector<uint32_t>({ annotationID }), util::make_unique<LiveTile>()));
auto tile_pos = annotationTiles.emplace(
tileID, std::make_pair(AnnotationIDs({ annotationID }),
util::make_unique<LiveTile>()));
// 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<std::weak_ptr<const LiveTileFeature>>({ feature }));
anno_it.first->second->tileFeatures.emplace(
tileID, std::vector<std::weak_ptr<const LiveTileFeature>>({ feature }));
z2 /= 2;
x /= 2;
@@ -102,23 +157,26 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
return std::make_pair(affectedTiles, annotationIDs);
}
std::vector<Tile::ID> AnnotationManager::removeAnnotations(std::vector<uint32_t> ids) {
std::vector<Tile::ID> AnnotationManager::removeAnnotations(const AnnotationIDs& ids) {
std::lock_guard<std::mutex> lock(mtx);
std::vector<Tile::ID> 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) {
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); });
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);
}
@@ -130,15 +188,18 @@ std::vector<Tile::ID> AnnotationManager::removeAnnotations(std::vector<uint32_t>
return affectedTiles;
}
std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(LatLngBounds queryBounds, const Map& map) const {
uint8_t z = map.getMaxZoom();
uint32_t z2 = 1 << z;
vec2<double> swPoint = projectPoint(queryBounds.sw);
vec2<double> nePoint = projectPoint(queryBounds.ne);
std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(const LatLngBounds& queryBounds,
const Map& map) const {
std::lock_guard<std::mutex> lock(mtx);
const uint8_t z = map.getMaxZoom();
const uint32_t z2 = 1 << z;
const vec2<double> swPoint = projectPoint(queryBounds.sw);
const vec2<double> 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<uint32_t> matchingAnnotations;
@@ -148,16 +209,23 @@ std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(LatLngBounds que
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));
std::copy(tile.second.first.begin(), tile.second.first.end(),
std::back_inserter(matchingAnnotations));
} else {
// check tile's annotations' bounding boxes
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;
}
});
}
}
@@ -167,10 +235,12 @@ std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(LatLngBounds que
return matchingAnnotations;
}
LatLngBounds AnnotationManager::getBoundsForAnnotations(std::vector<uint32_t> ids) const {
LatLngBounds AnnotationManager::getBoundsForAnnotations(const AnnotationIDs& ids) const {
std::lock_guard<std::mutex> 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());
}
@@ -179,12 +249,17 @@ LatLngBounds AnnotationManager::getBoundsForAnnotations(std::vector<uint32_t> id
return bounds;
}
const std::unique_ptr<LiveTile>& AnnotationManager::getTile(Tile::ID const& id) {
const LiveTile* AnnotationManager::getTile(Tile::ID const& id) {
std::lock_guard<std::mutex> 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";
}

View File

@@ -1,5 +1,6 @@
#include <mbgl/map/annotation.hpp>
#include <mbgl/map/live_tile_data.hpp>
#include <mbgl/map/live_tile.hpp>
#include <mbgl/map/tile_parser.hpp>
#include <mbgl/style/style_source.hpp>
#include <mbgl/map/vector_tile.hpp>
@@ -36,7 +37,7 @@ void LiveTileData::parse() {
}
if (source.type == SourceType::Annotations) {
const std::unique_ptr<LiveTile>& tile = annotationManager.getTile(id);
const LiveTile* tile = annotationManager.getTile(id);
if (tile) {
// Parsing creates state that is encapsulated in TileParser. While parsing,

View File

@@ -531,19 +531,19 @@ const 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<LatLng> points({ point });
std::vector<std::string> symbols({ symbol });
return addPointAnnotations(points, symbols)[0];
}
std::vector<uint32_t> Map::addPointAnnotations(std::vector<LatLng> points, std::vector<std::string>& symbols) {
std::vector<uint32_t> Map::addPointAnnotations(const std::vector<LatLng>& points, const std::vector<std::string>& symbols) {
assert(Environment::currentlyOn(ThreadType::Main));
auto result = annotationManager->addPointAnnotations(points, symbols, *this);
updateAnnotationTiles(result.first);
@@ -555,23 +555,24 @@ void Map::removeAnnotation(uint32_t annotation) {
removeAnnotations({ annotation });
}
void Map::removeAnnotations(std::vector<uint32_t> annotations) {
void Map::removeAnnotations(const std::vector<uint32_t>& annotations) {
assert(Environment::currentlyOn(ThreadType::Main));
auto result = annotationManager->removeAnnotations(annotations);
updateAnnotationTiles(result);
}
std::vector<uint32_t> Map::getAnnotationsInBounds(LatLngBounds bounds) const {
std::vector<uint32_t> Map::getAnnotationsInBounds(const LatLngBounds& bounds) const {
assert(Environment::currentlyOn(ThreadType::Main));
return annotationManager->getAnnotationsInBounds(bounds, *this);
}
LatLngBounds Map::getBoundsForAnnotations(std::vector<uint32_t> annotations) const {
LatLngBounds Map::getBoundsForAnnotations(const std::vector<uint32_t>& annotations) const {
assert(Environment::currentlyOn(ThreadType::Main));
return annotationManager->getBoundsForAnnotations(annotations);
}
void Map::updateAnnotationTiles(std::vector<Tile::ID>& ids) {
void Map::updateAnnotationTiles(const std::vector<Tile::ID>& ids) {
assert(Environment::currentlyOn(ThreadType::Main));
for (const auto &source : activeSources) {
if (source->info.type == SourceType::Annotations) {
source->source->invalidateTiles(*this, ids);

View File

@@ -373,7 +373,7 @@ void Source::update(Map &map,
updated = map.getTime();
}
void Source::invalidateTiles(Map& map, std::vector<Tile::ID>& ids) {
void Source::invalidateTiles(Map& map, const std::vector<Tile::ID>& ids) {
for (auto& id : ids) {
tiles.erase(id);
tile_data.erase(id);

View File

@@ -37,7 +37,7 @@ public:
void load(Map &, Environment &);
void update(Map &, uv::worker &, util::ptr<Style>, GlyphAtlas &, GlyphStore &,
SpriteAtlas &, util::ptr<Sprite>, TexturePool &, std::function<void()> callback);
void invalidateTiles(Map&, std::vector<Tile::ID>&);
void invalidateTiles(Map&, const std::vector<Tile::ID>&);
void updateMatrices(const mat4 &projMatrix, const TransformState &transform);
void drawClippingMasks(Painter &painter);

View File

@@ -1,6 +1,7 @@
#include <mbgl/style/style_source.hpp>
#include <mbgl/style/style_parser.hpp>
#include <mbgl/style/style_layer_group.hpp>
#include <mbgl/map/annotation.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/vec.hpp>
@@ -32,7 +33,7 @@ void StyleParser::parse(JSVal document) {
// create point annotations layer
//
std::string id = util::ANNOTATIONS_POINTS_LAYER_ID;
const std::string& id = AnnotationManager::layerID;
std::map<ClassID, ClassProperties> paints;
util::ptr<StyleLayer> annotations = std::make_shared<StyleLayer>(id, std::move(paints));

View File

@@ -8,8 +8,6 @@ const double mbgl::util::M2PI = 2 * M_PI;
const double mbgl::util::EARTH_RADIUS_M = 6378137;
const double mbgl::util::LATITUDE_MAX = 85.05112878;
const std::string mbgl::util::ANNOTATIONS_POINTS_LAYER_ID = "com.mapbox.annotations.points";
#if defined(DEBUG)
const bool mbgl::debug::tileParseWarnings = false;
const bool mbgl::debug::styleParseWarnings = false;