// Copyright 2016, University of Freiburg, // Chair of Algorithms and Data Structures. // Author: Patrick Brosi #ifndef UTIL_GEO_GRID_H_ #define UTIL_GEO_GRID_H_ #include #include #include #include "util/geo/Geo.h" namespace util { namespace geo { class GridException : public std::runtime_error { public: GridException(std::string const& msg) : std::runtime_error(msg) {} }; template class G, typename T> class Grid { public: Grid(const Grid&) = delete; Grid(Grid&& o) : _width(o._width), _height(o._height), _cellWidth(o._cellWidth), _cellHeight(o._cellHeight), _bb(o._bb), _xWidth(o._xWidth), _yHeight(o._yHeight), _hasValIdx(o._hasValIdx), _grid(o._grid), _index(o._index), _removed(o._removed) { o._grid = 0; } Grid& operator=(Grid&& o) { _width = o._width; _height = o._height; _cellWidth = o._cellWidth; _cellHeight = o._cellHeight; _bb = o._bb; _xWidth = o._xWidth; _yHeight = o._yHeight; _hasValIdx = o._hasValIdx; _grid = o._grid; _index = std::move(o._index); _removed = std::move(o._removed); o._grid = 0; return *this; }; // initialization of a point grid with cell width w and cell height h // that covers the area of bounding box bbox Grid(double w, double h, const Box& bbox); // initialization of a point grid with cell width w and cell height h // that covers the area of bounding box bbox // optional parameters specifies whether a value->cell index // should be kept (true by default!) Grid(double w, double h, const Box& bbox, bool buildValIdx); // the empty grid Grid(); // the empty grid Grid(bool buildValIdx); ~Grid() { if (!_grid) return; for (size_t i = 0; i < _xWidth; i++) { delete[] _grid[i]; } delete[] _grid; } // add object t to this grid void add(G geom, V val); void add(size_t x, size_t y, V val); void get(const Box& btbox, std::set* s) const; void get(const G& geom, double d, std::set* s) const; void get(size_t x, size_t y, std::set* s) const; void remove(V val); void getNeighbors(const V& val, double d, std::set* s) const; void getCellNeighbors(const V& val, size_t d, std::set* s) const; void getCellNeighbors(size_t x, size_t y, size_t xPerm, size_t yPerm, std::set* s) const; std::set > getCells(const V& val) const; size_t getXWidth() const; size_t getYHeight() const; size_t getCellXFromX(double lon) const; size_t getCellYFromY(double lat) const; private: double _width; double _height; double _cellWidth; double _cellHeight; Box _bb; size_t _xWidth; size_t _yHeight; bool _hasValIdx; // raw 2d array, less memory overhead std::set** _grid; std::map > > _index; std::set _removed; Box getBox(size_t x, size_t y) const; }; #include "util/geo/Grid.tpp" } // namespace geo } // namespace util #endif // UTIL_GEO_GRID_H_