#pragma once #include #include #include #include #include #include // TODO: Move into geometry.hpp project namespace mapbox { namespace geometry { template struct circle { using point_type = point; constexpr circle(point_type const& center_, T const& radius_) : center(center_), radius(radius_) {} point_type center; T radius; }; template constexpr bool operator==(circle const& lhs, circle const& rhs) { return lhs.center == rhs.center && lhs.radius == rhs.radius; } template constexpr bool operator!=(circle const& lhs, circle const& rhs) { return lhs.center != rhs.center || lhs.radius != rhs.radius; } } // namespace geometry } // namespace mapbox namespace mbgl { /* GridIndex is a data structure for testing the intersection of circles and rectangles in a 2d plane. It is optimized for rapid insertion and querying. GridIndex splits the plane into a set of "cells" and keeps track of which geometries intersect with each cell. At query time, full geometry comparisons are only done for items that share at least one cell. As long as the geometries are relatively uniformly distributed across the plane, this greatly reduces the number of comparisons necessary. */ template class GridIndex { public: GridIndex(int32_t width_, int32_t height_, int32_t cellSize_); using BBox = mapbox::geometry::box; using BCircle = mapbox::geometry::circle; void insert(T&& t, const BBox&); void insert(T&& t, const BCircle&); std::vector query(const BBox&) const; std::vector query(const BCircle&) const; bool hitTest(const BBox&) const; bool hitTest(const BCircle&) const; private: void query(const BBox&, std::function) const; void query(const BCircle&, std::function) const; int32_t convertToXCellCoord(int32_t x) const; int32_t convertToYCellCoord(int32_t y) const; bool boxesCollide(const BBox&, const BBox&) const; bool circlesCollide(const BCircle&, const BCircle&) const; bool circleAndBoxCollide(const BCircle&, const BBox&) const; const int32_t width; const int32_t height; const int16_t xCellCount; const int16_t yCellCount; const double xScale; const double yScale; std::vector> boxElements; std::vector> circleElements; std::vector> boxCells; std::vector> circleCells; }; } // namespace mbgl