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

Commit

Permalink
[core] Use different query boxes for points vs shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
boundsj committed Aug 26, 2016
1 parent 4aa6e6e commit bae2717
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
9 changes: 6 additions & 3 deletions src/mbgl/geometry/feature_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static bool topDownSymbols(const IndexedSubfeature& a, const IndexedSubfeature&

void FeatureIndex::query(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCollection& scaledQueryGeometry,
const GeometryCollection& queryGeometry,
const float bearing,
const double tileSize,
Expand All @@ -61,10 +62,10 @@ void FeatureIndex::query(
const CanonicalTileID& tileID,
const style::Style& style) const {

mapbox::geometry::box<int16_t> box = mapbox::geometry::envelope(queryGeometry);

const float pixelsToTileUnits = util::EXTENT / tileSize / scale;
const int16_t additionalRadius = std::min<int16_t>(util::EXTENT, std::ceil(style.getQueryRadius() * pixelsToTileUnits));
mapbox::geometry::box<int16_t> box = mapbox::geometry::envelope(queryGeometry);
std::vector<IndexedSubfeature> features = grid.query({ box.min - additionalRadius, box.max + additionalRadius });

std::sort(features.begin(), features.end(), topDown);
Expand All @@ -78,12 +79,14 @@ void FeatureIndex::query(
addFeature(result, indexedFeature, queryGeometry, filterLayerIDs, geometryTileData, tileID, style, bearing, pixelsToTileUnits);
}

mapbox::geometry::box<int16_t> scaledBox = mapbox::geometry::envelope(scaledQueryGeometry);

// query symbol features
assert(collisionTile);
std::vector<IndexedSubfeature> symbolFeatures = collisionTile->queryRenderedSymbols(box, scale);
std::vector<IndexedSubfeature> symbolFeatures = collisionTile->queryRenderedSymbols(scaledBox, scale);
std::sort(symbolFeatures.begin(), symbolFeatures.end(), topDownSymbols);
for (const auto& symbolFeature : symbolFeatures) {
addFeature(result, symbolFeature, queryGeometry, filterLayerIDs, geometryTileData, tileID, style, bearing, pixelsToTileUnits);
addFeature(result, symbolFeature, scaledQueryGeometry, filterLayerIDs, geometryTileData, tileID, style, bearing, pixelsToTileUnits);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/mbgl/geometry/feature_index.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class FeatureIndex {

void query(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCollection& scaledQueryGeometry,
const GeometryCollection& queryGeometry,
const float bearing,
const double tileSize,
Expand Down
38 changes: 28 additions & 10 deletions src/mbgl/style/source_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,40 +221,58 @@ std::unordered_map<std::string, std::vector<Feature>> Source::Impl::queryRendere
scaledScreenBox.max.x *= scale;
scaledScreenBox.max.y *= scale;

LineString<double> geometry = {
LineString<double> scaledGeometry = {
scaledScreenBox.min,
{ scaledScreenBox.max.x, scaledScreenBox.min.y },
scaledScreenBox.max,
{ scaledScreenBox.min.x, scaledScreenBox.max.y },
scaledScreenBox.min
};

LineString<double> queryGeometry;
LineString<double> geometry = {
parameters.box.min,
{ parameters.box.max.x, parameters.box.min.y },
parameters.box.max,
{ parameters.box.min.x, parameters.box.max.y },
parameters.box.min
};

for (const auto& p : geometry) {
queryGeometry.push_back(TileCoordinate::fromScreenCoordinate(
LineString<double> scaledQueryGeometry;

for (const auto& p : scaledGeometry) {
scaledQueryGeometry.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()) {
if (scaledQueryGeometry.empty()) {
return result;
}

LineString<double> queryGeometry;

for (const auto& p : 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(scaledQueryGeometry);
Point<int16_t> tileSpaceBoundsMin = coordinateToTilePoint(tile.id, box.min);
Point<int16_t> tileSpaceBoundsMax = coordinateToTilePoint(tile.id, box.max);

if (tileSpaceBoundsMin.x >= util::EXTENT || tileSpaceBoundsMin.y >= util::EXTENT ||
tileSpaceBoundsMax.x < 0 || tileSpaceBoundsMax.y < 0) continue;

GeometryCoordinates tileSpaceQueryGeometryScaled;
for (const auto& c : scaledQueryGeometry) {
tileSpaceQueryGeometryScaled.push_back(coordinateToTilePoint(tile.id, c));
}

GeometryCoordinates tileSpaceQueryGeometry;

for (const auto& c : queryGeometry) {
tileSpaceQueryGeometry.push_back(coordinateToTilePoint(tile.id, c));
}

tile.tile.queryRenderedFeatures(result,
tileSpaceQueryGeometryScaled,
tileSpaceQueryGeometry,
parameters.transformState,
parameters.layerIDs);
Expand Down
2 changes: 2 additions & 0 deletions src/mbgl/tile/geometry_tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,15 @@ void GeometryTile::redoPlacement() {

void GeometryTile::queryRenderedFeatures(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCoordinates& scaledQueryGeometry,
const GeometryCoordinates& queryGeometry,
const TransformState& transformState,
const optional<std::vector<std::string>>& layerIDs) {

if (!featureIndex || !data) return;

featureIndex->query(result,
{ scaledQueryGeometry },
{ queryGeometry },
transformState.getAngle(),
util::tileSize * id.overscaleFactor(),
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/tile/geometry_tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GeometryTile : public Tile {

void queryRenderedFeatures(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCoordinates& scaledQueryGeometry,
const GeometryCoordinates& queryGeometry,
const TransformState&,
const optional<std::vector<std::string>>& layerIDs) override;
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/tile/tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void Tile::dumpDebugLogs() const {

void Tile::queryRenderedFeatures(
std::unordered_map<std::string, std::vector<Feature>>&,
const GeometryCoordinates& scaledQueryGeometry,
const GeometryCoordinates&,
const TransformState&,
const optional<std::vector<std::string>>&) {}
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/tile/tile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class Tile : private util::noncopyable {

virtual void queryRenderedFeatures(
std::unordered_map<std::string, std::vector<Feature>>& result,
const GeometryCoordinates& scaledQueryGeometry,
const GeometryCoordinates& queryGeometry,
const TransformState&,
const optional<std::vector<std::string>>& layerIDs);
Expand Down

0 comments on commit bae2717

Please sign in to comment.