Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Micro-optimizations in geometry code
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Jun 30, 2016
1 parent 03f30f5 commit d8c0488
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/mbgl/annotation/shape_annotation_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, Annotati
}

const auto& shapeTile = shapeTiler->getTile(tileID.z, tileID.x, tileID.y);
if (!shapeTile)
if (!shapeTile) {
return;
}

AnnotationTileLayer& layer = *data.layers.emplace(layerID,
std::make_unique<AnnotationTileLayer>(layerID)).first->second;

for (auto& shapeFeature : shapeTile.features) {
for (const auto& shapeFeature : shapeTile.features) {
FeatureType featureType = FeatureType::Unknown;

if (shapeFeature.type == geojsonvt::TileFeatureType::LineString) {
Expand All @@ -124,10 +125,10 @@ void ShapeAnnotationImpl::updateTileData(const CanonicalTileID& tileID, Annotati
assert(featureType != FeatureType::Unknown);

GeometryCollection renderGeometry;
for (auto& shapeRing : shapeFeature.tileGeometry.get<geojsonvt::TileRings>()) {
for (const auto& shapeRing : shapeFeature.tileGeometry.get<geojsonvt::TileRings>()) {
GeometryCoordinates renderLine;

for (auto& shapePoint : shapeRing) {
for (const auto& shapePoint : shapeRing) {
renderLine.emplace_back(shapePoint.x, shapePoint.y);
}

Expand Down
11 changes: 9 additions & 2 deletions src/mbgl/style/source_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,23 @@ static Point<int16_t> coordinateToTilePoint(const UnwrappedTileID& tileID, const
}

std::unordered_map<std::string, std::vector<Feature>> Source::Impl::queryRenderedFeatures(const QueryParameters& parameters) const {
std::unordered_map<std::string, std::vector<Feature>> result;
if (renderTiles.empty()) {
return result;
}

LineString<double> queryGeometry;

for (const auto& p : parameters.geometry) {
queryGeometry.push_back(TileCoordinate::fromScreenCoordinate(
parameters.transformState, 0, { p.x, parameters.transformState.getHeight() - p.y }).p);
}

mapbox::geometry::box<double> box = mapbox::geometry::envelope(queryGeometry);
if (queryGeometry.empty()) {
return result;
}

std::unordered_map<std::string, std::vector<Feature>> result;
mapbox::geometry::box<double> box = mapbox::geometry::envelope(queryGeometry);

for (const auto& tilePtr : renderTiles) {
const RenderTile& tile = tilePtr.second;
Expand Down
5 changes: 4 additions & 1 deletion src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,14 +337,17 @@ RenderData Style::getRenderData(MapDebugOptions debugOptions) const {
}

std::vector<Feature> Style::queryRenderedFeatures(const QueryParameters& parameters) const {
std::vector<Feature> result;
std::unordered_map<std::string, std::vector<Feature>> resultsByLayer;

for (const auto& source : sources) {
auto sourceResults = source->baseImpl->queryRenderedFeatures(parameters);
std::move(sourceResults.begin(), sourceResults.end(), std::inserter(resultsByLayer, resultsByLayer.begin()));
}

std::vector<Feature> result;
if (resultsByLayer.empty()) {
return result;
}

// Combine all results based on the style layer order.
for (const auto& layer : layers) {
Expand Down
18 changes: 11 additions & 7 deletions src/mbgl/util/grid_index.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <mbgl/util/grid_index.hpp>
#include <mbgl/geometry/feature_index.hpp>
#include <mbgl/math/minmax.hpp>

#include <unordered_set>

Expand Down Expand Up @@ -28,9 +29,10 @@ void GridIndex<T>::insert(T&& t, const BBox& bbox) {
auto cx2 = convertToCellCoord(bbox.max.x);
auto cy2 = convertToCellCoord(bbox.max.y);

for (int32_t x = cx1; x <= cx2; x++) {
for (int32_t y = cy1; y <= cy2; y++) {
auto cellIndex = d * y + x;
int32_t x, y, cellIndex;
for (x = cx1; x <= cx2; ++x) {
for (y = cy1; y <= cy2; ++y) {
cellIndex = d * y + x;
cells[cellIndex].push_back(uid);
}
}
Expand All @@ -48,9 +50,10 @@ std::vector<T> GridIndex<T>::query(const BBox& queryBBox) const {
auto cx2 = convertToCellCoord(queryBBox.max.x);
auto cy2 = convertToCellCoord(queryBBox.max.y);

for (int32_t x = cx1; x <= cx2; x++) {
for (int32_t y = cy1; y <= cy2; y++) {
auto cellIndex = d * y + x;
int32_t x, y, cellIndex;
for (x = cx1; x <= cx2; ++x) {
for (y = cy1; y <= cy2; ++y) {
cellIndex = d * y + x;
for (auto uid : cells[cellIndex]) {
if (seenUids.count(uid) == 0) {
seenUids.insert(uid);
Expand All @@ -75,8 +78,9 @@ std::vector<T> GridIndex<T>::query(const BBox& queryBBox) const {

template <class T>
int32_t GridIndex<T>::convertToCellCoord(int32_t x) const {
return std::max(0.0, std::min(d - 1.0, std::floor(x * scale) + padding));
return util::max(0.0, util::min(d - 1.0, std::floor(x * scale) + padding));
}

template class GridIndex<IndexedSubfeature>;

} // namespace mbgl

0 comments on commit d8c0488

Please sign in to comment.