// Copyright 2017, University of Freiburg, // Chair of Algorithms and Data Structures. // Author: Patrick Brosi // _____________________________________________________________________________ template class G, typename T> Grid::Grid(bool bldIdx) : _width(0), _height(0), _cellWidth(0), _cellHeight(0), _xWidth(0), _yHeight(0), _hasValIdx(bldIdx), _grid(0) {} // _____________________________________________________________________________ template class G, typename T> Grid::Grid() : Grid(true) {} // _____________________________________________________________________________ template class G, typename T> Grid::Grid(double w, double h, const Box& bbox) : Grid(w, h, bbox, true) {} // _____________________________________________________________________________ template class G, typename T> Grid::Grid(double w, double h, const Box& bbox, bool bValIdx) : _cellWidth(fabs(w)), _cellHeight(fabs(h)), _bb(bbox), _hasValIdx(bValIdx), _grid(0) { _width = bbox.getUpperRight().getX() - bbox.getLowerLeft().getX(); _height = bbox.getUpperRight().getY() - bbox.getLowerLeft().getY(); if (_width < 0 || _height < 0) { _width = 0; _height = 0; _xWidth = 0; _yHeight = 0; return; } _xWidth = ceil(_width / _cellWidth); _yHeight = ceil(_height / _cellHeight); // resize rows _grid = new std::set*[_xWidth]; // resize columns for (size_t x = 0; x < _xWidth; x++) { _grid[x] = new std::set[_yHeight]; } } // _____________________________________________________________________________ template class G, typename T> void Grid::add(G geom, V val) { Box box = getBoundingBox(geom); size_t swX = getCellXFromX(box.getLowerLeft().getX()); size_t swY = getCellYFromY(box.getLowerLeft().getY()); size_t neX = getCellXFromX(box.getUpperRight().getX()); size_t neY = getCellYFromY(box.getUpperRight().getY()); for (size_t x = swX; x <= neX && x < _xWidth; x++) { for (size_t y = swY; y <= neY && y < _yHeight; y++) { if (intersects(geom, getBox(x, y))) { add(x, y, val); } } } } // _____________________________________________________________________________ template class G, typename T> void Grid::add(size_t x, size_t y, V val) { _grid[x][y].insert(val); if (_hasValIdx) _index[val].insert(std::pair(x, y)); } // _____________________________________________________________________________ template class G, typename T> void Grid::get(const Box& box, std::set* s) const { size_t swX = getCellXFromX(box.getLowerLeft().getX()); size_t swY = getCellYFromY(box.getLowerLeft().getY()); size_t neX = getCellXFromX(box.getUpperRight().getX()); size_t neY = getCellYFromY(box.getUpperRight().getY()); for (size_t x = swX; x <= neX && x < _xWidth; x++) for (size_t y = swY; y <= neY && y < _yHeight; y++) get(x, y, s); } // _____________________________________________________________________________ template class G, typename T> template