improve old grid cells

This commit is contained in:
Andrey Chulkov 2020-05-21 20:57:39 +03:00
parent 85779fad97
commit 37e55e67dd
3 changed files with 39 additions and 42 deletions

View File

@ -76,22 +76,25 @@ std::shared_ptr<const IExternalLoadable> GridPolygonDictionary::clone() const
bool GridPolygonDictionary::find(const Point &point, size_t & id) const
{
bool found = false;
auto cell = grid.find(point.get<0>(), point.get<1>());
auto cell = grid.find(point.x(), point.y());
if (cell)
{
for (size_t i = 0; i < (cell->polygon_ids).size(); ++i)
{
const auto & candidate = (cell->polygon_ids)[i];
if ((cell->is_covered_by)[i] || bg::covered_by(point, polygons[candidate]))
if (bg::covered_by(point, polygons[candidate]))
{
found = true;
id = candidate;
break;
return true;
}
}
if (cell->first_covered != FinalCell::kNone)
{
id = cell->first_covered;
return true;
}
}
return found;
return false;
}
SmartPolygonDictionary::SmartPolygonDictionary(
@ -128,42 +131,26 @@ std::shared_ptr<const IExternalLoadable> SmartPolygonDictionary::clone() const
bool SmartPolygonDictionary::find(const Point & point, size_t & id) const
{
/*
bool found = false;
double area = 0;
for (size_t i = 0; i < polygons.size(); ++i)
{
size_t unused;
if (buckets[i].find(point, unused))
{
double new_area = areas[i];
if (!found || new_area < area)
{
found = true;
id = i;
area = new_area;
}
}
}
return found;
*/
bool found = false;
auto cell = grid.find(point.get<0>(), point.get<1>());
auto cell = grid.find(point.x(), point.y());
if (cell)
{
for (size_t i = 0; i < (cell->polygon_ids).size(); ++i)
{
const auto & candidate = (cell->polygon_ids)[i];
size_t unused;
if ((cell->is_covered_by)[i] || buckets[candidate].find(point, unused))
if (buckets[candidate].find(point, unused))
{
found = true;
id = candidate;
break;
return true;
}
}
if (cell->first_covered != FinalCell::kNone)
{
id = cell->first_covered;
return true;
}
}
return found;
return false;
}
OneBucketPolygonDictionary::OneBucketPolygonDictionary(
@ -194,12 +181,13 @@ std::shared_ptr<const IExternalLoadable> OneBucketPolygonDictionary::clone() con
bool OneBucketPolygonDictionary::find(const Point & point, size_t & id) const
{
auto cell = index.find(point.x(), point.y());
if (cell != nullptr) {
if (!cell->corresponding_ids.empty() && cell->index.find(point, id)) {
if (cell) {
if (!(cell->corresponding_ids).empty() && cell->index.find(point, id)) {
id = cell->corresponding_ids[id];
return true;
}
if (cell->first_covered != static_cast<size_t>(-1)) {
if (cell->first_covered != FinalCellWithSlabs::kNone)
{
id = cell->first_covered;
return true;
}

View File

@ -11,15 +11,19 @@
namespace DB
{
FinalCell::FinalCell(const std::vector<size_t> & polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_):
polygon_ids(polygon_ids_)
FinalCell::FinalCell(const std::vector<size_t> & polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_)
{
Polygon tmp_poly;
bg::convert(box_, tmp_poly);
std::transform(polygon_ids.begin(), polygon_ids.end(), std::back_inserter(is_covered_by), [&](const auto id)
for (const auto id : polygon_ids_)
{
return bg::covered_by(tmp_poly, polygons_[id]);
});
if (bg::covered_by(tmp_poly, polygons_[id]))
{
first_covered = id;
break;
}
polygon_ids.push_back(id);
}
}
const FinalCell * FinalCell::find(Coord, Coord) const
@ -32,7 +36,8 @@ FinalCellWithSlabs::FinalCellWithSlabs(const std::vector<size_t> & polygon_ids_,
Polygon tmp_poly;
bg::convert(box_, tmp_poly);
std::vector<Polygon> intersections;
for (const auto id : polygon_ids_) {
for (const auto id : polygon_ids_)
{
if (bg::covered_by(tmp_poly, polygons_[id]))
{
first_covered = id;

View File

@ -124,7 +124,9 @@ class FinalCell : public ICell<FinalCell>
public:
explicit FinalCell(const std::vector<size_t> & polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_);
std::vector<size_t> polygon_ids;
std::vector<uint8_t> is_covered_by;
size_t first_covered = kNone;
static constexpr size_t kNone = -1;
private:
[[nodiscard]] const FinalCell * find(Coord x, Coord y) const override;
@ -137,7 +139,9 @@ public:
BucketsPolygonIndex index;
std::vector<size_t> corresponding_ids;
size_t first_covered = -1;
size_t first_covered = kNone;
static constexpr size_t kNone = -1;
private:
[[nodiscard]] const FinalCellWithSlabs * find(Coord x, Coord y) const override;