2017-09-20 02:30:44 +00:00
|
|
|
#pragma once
|
2019-02-10 17:40:52 +00:00
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/types.h>
|
2017-09-20 02:30:44 +00:00
|
|
|
#include <Core/Defines.h>
|
2021-11-25 20:55:02 +00:00
|
|
|
#include <base/TypeLists.h>
|
2017-09-20 02:30:44 +00:00
|
|
|
#include <Columns/IColumn.h>
|
|
|
|
#include <Columns/ColumnVector.h>
|
|
|
|
#include <Common/typeid_cast.h>
|
2021-07-09 03:06:21 +00:00
|
|
|
#include <Common/NaNUtils.h>
|
2020-05-22 19:37:56 +00:00
|
|
|
#include <Common/SipHash.h>
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/range.h>
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
/// Warning in boost::geometry during template strategy substitution.
|
|
|
|
#pragma GCC diagnostic push
|
2017-12-02 02:47:12 +00:00
|
|
|
|
2021-04-18 09:17:02 +00:00
|
|
|
#if !defined(__clang__)
|
2017-09-20 02:30:44 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
|
|
|
|
2017-12-02 02:47:12 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
#include <boost/geometry.hpp>
|
|
|
|
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
|
|
|
#include <boost/geometry/geometries/point_xy.hpp>
|
|
|
|
#include <boost/geometry/geometries/polygon.hpp>
|
|
|
|
#include <boost/geometry/geometries/multi_polygon.hpp>
|
|
|
|
#include <boost/geometry/geometries/segment.hpp>
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <vector>
|
|
|
|
#include <iterator>
|
|
|
|
#include <cmath>
|
|
|
|
#include <algorithm>
|
2020-05-22 19:27:38 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-03-26 08:15:48 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int LOGICAL_ERROR;
|
2021-07-09 03:49:08 +00:00
|
|
|
extern const int BAD_ARGUMENTS;
|
2020-03-26 08:15:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
template <typename Polygon>
|
2017-09-23 23:47:43 +00:00
|
|
|
UInt64 getPolygonAllocatedBytes(const Polygon & polygon)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
UInt64 size = 0;
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
using RingType = typename Polygon::ring_type;
|
|
|
|
using ValueType = typename RingType::value_type;
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
auto sizeOfRing = [](const RingType & ring) { return sizeof(ring) + ring.capacity() * sizeof(ValueType); };
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
size += sizeOfRing(polygon.outer());
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
const auto & inners = polygon.inners();
|
2017-09-23 23:47:43 +00:00
|
|
|
size += sizeof(inners) + inners.capacity() * sizeof(RingType);
|
2017-09-20 02:30:44 +00:00
|
|
|
for (auto & inner : inners)
|
2017-09-23 23:47:43 +00:00
|
|
|
size += sizeOfRing(inner);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename MultiPolygon>
|
2017-09-23 23:47:43 +00:00
|
|
|
UInt64 getMultiPolygonAllocatedBytes(const MultiPolygon & multi_polygon)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2017-09-23 23:47:43 +00:00
|
|
|
using ValueType = typename MultiPolygon::value_type;
|
|
|
|
UInt64 size = multi_polygon.capacity() * sizeof(ValueType);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
for (const auto & polygon : multi_polygon)
|
2017-09-23 23:47:43 +00:00
|
|
|
size += getPolygonAllocatedBytes(polygon);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2020-03-25 19:44:16 +00:00
|
|
|
|
|
|
|
/// This algorithm can be used as a baseline for comparison.
|
|
|
|
template <typename CoordinateType>
|
|
|
|
class PointInPolygonTrivial
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Point = boost::geometry::model::d2::point_xy<CoordinateType>;
|
|
|
|
/// Counter-Clockwise ordering.
|
|
|
|
using Polygon = boost::geometry::model::polygon<Point, false>;
|
|
|
|
using MultiPolygon = boost::geometry::model::multi_polygon<Polygon>;
|
|
|
|
using Box = boost::geometry::model::box<Point>;
|
|
|
|
using Segment = boost::geometry::model::segment<Point>;
|
|
|
|
|
|
|
|
explicit PointInPolygonTrivial(const Polygon & polygon_)
|
|
|
|
: polygon(polygon_) {}
|
|
|
|
|
|
|
|
/// True if bound box is empty.
|
|
|
|
bool hasEmptyBound() const { return false; }
|
|
|
|
|
|
|
|
UInt64 getAllocatedBytes() const { return 0; }
|
|
|
|
|
|
|
|
bool contains(CoordinateType x, CoordinateType y) const
|
|
|
|
{
|
|
|
|
return boost::geometry::covered_by(Point(x, y), polygon);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Polygon polygon;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Simple algorithm with bounding box.
|
2020-05-22 21:47:31 +00:00
|
|
|
template <typename Strategy, typename CoordinateType>
|
2020-03-25 19:44:16 +00:00
|
|
|
class PointInPolygon
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Point = boost::geometry::model::d2::point_xy<CoordinateType>;
|
|
|
|
/// Counter-Clockwise ordering.
|
|
|
|
using Polygon = boost::geometry::model::polygon<Point, false>;
|
|
|
|
using Box = boost::geometry::model::box<Point>;
|
|
|
|
|
|
|
|
explicit PointInPolygon(const Polygon & polygon_) : polygon(polygon_)
|
|
|
|
{
|
|
|
|
boost::geometry::envelope(polygon, box);
|
|
|
|
|
|
|
|
const Point & min_corner = box.min_corner();
|
|
|
|
const Point & max_corner = box.max_corner();
|
|
|
|
|
|
|
|
if (min_corner.x() == max_corner.x() || min_corner.y() == max_corner.y())
|
|
|
|
has_empty_bound = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasEmptyBound() const { return has_empty_bound; }
|
|
|
|
|
|
|
|
inline bool ALWAYS_INLINE contains(CoordinateType x, CoordinateType y) const
|
|
|
|
{
|
|
|
|
Point point(x, y);
|
|
|
|
|
|
|
|
if (!boost::geometry::within(point, box))
|
|
|
|
return false;
|
|
|
|
|
2020-05-22 21:47:31 +00:00
|
|
|
return boost::geometry::covered_by(point, polygon, strategy);
|
2020-03-25 19:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UInt64 getAllocatedBytes() const { return sizeof(*this); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const Polygon & polygon;
|
|
|
|
Box box;
|
|
|
|
bool has_empty_bound = false;
|
2020-05-22 21:47:31 +00:00
|
|
|
Strategy strategy;
|
2020-03-25 19:44:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Optimized algorithm with bounding box and grid.
|
2020-03-25 17:29:26 +00:00
|
|
|
template <typename CoordinateType>
|
2017-09-20 02:30:44 +00:00
|
|
|
class PointInPolygonWithGrid
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using Point = boost::geometry::model::d2::point_xy<CoordinateType>;
|
|
|
|
/// Counter-Clockwise ordering.
|
|
|
|
using Polygon = boost::geometry::model::polygon<Point, false>;
|
|
|
|
using MultiPolygon = boost::geometry::model::multi_polygon<Polygon>;
|
|
|
|
using Box = boost::geometry::model::box<Point>;
|
|
|
|
using Segment = boost::geometry::model::segment<Point>;
|
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
explicit PointInPolygonWithGrid(const Polygon & polygon_, UInt16 grid_size_ = 8)
|
2020-03-25 19:14:29 +00:00
|
|
|
: grid_size(std::max<UInt16>(1, grid_size_)), polygon(polygon_)
|
|
|
|
{
|
|
|
|
buildGrid();
|
|
|
|
}
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
/// True if bound box is empty.
|
2017-09-20 02:30:44 +00:00
|
|
|
bool hasEmptyBound() const { return has_empty_bound; }
|
|
|
|
|
|
|
|
UInt64 getAllocatedBytes() const;
|
|
|
|
|
2020-03-25 19:07:10 +00:00
|
|
|
inline bool ALWAYS_INLINE contains(CoordinateType x, CoordinateType y) const;
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum class CellType
|
|
|
|
{
|
2020-03-25 19:44:16 +00:00
|
|
|
inner, /// The cell is completely inside polygon.
|
|
|
|
outer, /// The cell is completely outside of polygon.
|
2020-08-08 00:47:03 +00:00
|
|
|
singleLine, /// The cell is split to inner/outer part by a single line.
|
|
|
|
pairOfLinesSingleConvexPolygon, /// The cell is split to inner/outer part by a polyline of two sections and inner part is convex.
|
|
|
|
pairOfLinesSingleNonConvexPolygons, /// The cell is split to inner/outer part by a polyline of two sections and inner part is non convex.
|
2020-03-25 19:44:16 +00:00
|
|
|
pairOfLinesDifferentPolygons, /// The cell is spliited by two lines to three different parts.
|
|
|
|
complexPolygon /// Generic case.
|
2017-09-20 02:30:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct HalfPlane
|
|
|
|
{
|
2017-09-23 23:47:43 +00:00
|
|
|
/// Line, a * x + b * y + c = 0. Vector (a, b) points inside half-plane.
|
2017-09-20 02:30:44 +00:00
|
|
|
CoordinateType a;
|
|
|
|
CoordinateType b;
|
|
|
|
CoordinateType c;
|
|
|
|
|
2020-03-25 20:12:32 +00:00
|
|
|
HalfPlane() = default;
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
/// Take left half-plane.
|
2020-03-25 20:12:32 +00:00
|
|
|
HalfPlane(const Point & from, const Point & to)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
a = -(to.y() - from.y());
|
|
|
|
b = to.x() - from.x();
|
|
|
|
c = -from.x() * a - from.y() * b;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inner part of the HalfPlane is the left side of initialized vector.
|
|
|
|
bool ALWAYS_INLINE contains(CoordinateType x, CoordinateType y) const { return a * x + b * y + c >= 0; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Cell
|
|
|
|
{
|
2017-09-23 23:47:43 +00:00
|
|
|
static const int max_stored_half_planes = 2;
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
HalfPlane half_planes[max_stored_half_planes];
|
2017-09-20 02:30:44 +00:00
|
|
|
size_t index_of_inner_polygon;
|
|
|
|
CellType type;
|
|
|
|
};
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
const UInt16 grid_size;
|
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
Polygon polygon;
|
2017-09-23 23:47:43 +00:00
|
|
|
std::vector<Cell> cells;
|
2017-09-20 02:30:44 +00:00
|
|
|
std::vector<MultiPolygon> polygons;
|
|
|
|
|
|
|
|
CoordinateType cell_width;
|
|
|
|
CoordinateType cell_height;
|
|
|
|
|
|
|
|
CoordinateType x_shift;
|
|
|
|
CoordinateType y_shift;
|
|
|
|
CoordinateType x_scale;
|
|
|
|
CoordinateType y_scale;
|
|
|
|
|
|
|
|
bool has_empty_bound = false;
|
|
|
|
|
|
|
|
void buildGrid();
|
|
|
|
|
2020-03-25 19:19:49 +00:00
|
|
|
/// Calculate bounding box and shift/scale of cells.
|
2017-09-20 02:30:44 +00:00
|
|
|
void calcGridAttributes(Box & box);
|
|
|
|
|
|
|
|
template <typename T>
|
2017-09-23 23:47:43 +00:00
|
|
|
T ALWAYS_INLINE getCellIndex(T row, T col) const { return row * grid_size + col; }
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
/// Complex case. Will check intersection directly.
|
|
|
|
inline void addComplexPolygonCell(size_t index, const Box & box);
|
|
|
|
|
|
|
|
/// Empty intersection or intersection == box.
|
|
|
|
inline void addCell(size_t index, const Box & empty_box);
|
|
|
|
|
|
|
|
/// Intersection is a single polygon.
|
|
|
|
inline void addCell(size_t index, const Box & box, const Polygon & intersection);
|
|
|
|
|
|
|
|
/// Intersection is a pair of polygons.
|
|
|
|
inline void addCell(size_t index, const Box & box, const Polygon & first, const Polygon & second);
|
|
|
|
|
|
|
|
/// Returns a list of half-planes were formed from intersection edges without box edges.
|
|
|
|
inline std::vector<HalfPlane> findHalfPlanes(const Box & box, const Polygon & intersection);
|
|
|
|
|
2018-08-21 09:56:01 +00:00
|
|
|
/// Check that polygon.outer() is convex.
|
|
|
|
inline bool isConvex(const Polygon & polygon);
|
2017-09-20 02:30:44 +00:00
|
|
|
};
|
|
|
|
|
2020-03-25 19:07:10 +00:00
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
UInt64 PointInPolygonWithGrid<CoordinateType>::getAllocatedBytes() const
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
UInt64 size = sizeof(*this);
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
size += cells.capacity() * sizeof(Cell);
|
|
|
|
size += polygons.capacity() * sizeof(MultiPolygon);
|
|
|
|
size += getPolygonAllocatedBytes(polygon);
|
|
|
|
|
2019-01-04 12:10:00 +00:00
|
|
|
for (const auto & elem : polygons)
|
|
|
|
size += getMultiPolygonAllocatedBytes(elem);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
void PointInPolygonWithGrid<CoordinateType>::calcGridAttributes(
|
|
|
|
PointInPolygonWithGrid<CoordinateType>::Box & box)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
boost::geometry::envelope(polygon, box);
|
|
|
|
|
|
|
|
const Point & min_corner = box.min_corner();
|
|
|
|
const Point & max_corner = box.max_corner();
|
2019-07-01 15:01:10 +00:00
|
|
|
|
2019-07-01 14:42:05 +00:00
|
|
|
#pragma GCC diagnostic push
|
2021-04-18 09:17:02 +00:00
|
|
|
#if !defined(__clang__)
|
2019-07-01 14:42:05 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
|
|
|
|
2019-07-01 14:44:45 +00:00
|
|
|
cell_width = (max_corner.x() - min_corner.x()) / grid_size;
|
|
|
|
cell_height = (max_corner.y() - min_corner.y()) / grid_size;
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2019-07-01 14:42:05 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
if (cell_width == 0 || cell_height == 0)
|
|
|
|
{
|
|
|
|
has_empty_bound = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x_scale = 1 / cell_width;
|
|
|
|
y_scale = 1 / cell_height;
|
|
|
|
x_shift = -min_corner.x();
|
|
|
|
y_shift = -min_corner.y();
|
2021-07-09 03:06:21 +00:00
|
|
|
|
|
|
|
if (!(isFinite(x_scale)
|
|
|
|
&& isFinite(y_scale)
|
|
|
|
&& isFinite(x_shift)
|
|
|
|
&& isFinite(y_shift)
|
|
|
|
&& isFinite(grid_size)))
|
|
|
|
throw Exception("Polygon is not valid: bounding box is unbounded", ErrorCodes::BAD_ARGUMENTS);
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
void PointInPolygonWithGrid<CoordinateType>::buildGrid()
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
Box box;
|
|
|
|
calcGridAttributes(box);
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
if (has_empty_bound)
|
|
|
|
return;
|
|
|
|
|
2020-07-04 23:16:16 +00:00
|
|
|
cells.assign(size_t(grid_size) * grid_size, {});
|
2017-09-23 23:47:43 +00:00
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
const Point & min_corner = box.min_corner();
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
for (size_t row = 0; row < grid_size; ++row)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2019-12-27 05:51:52 +00:00
|
|
|
#pragma GCC diagnostic push
|
2021-04-18 09:17:02 +00:00
|
|
|
#if !defined(__clang__)
|
2019-12-27 05:51:52 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
|
|
|
#endif
|
2017-09-20 02:30:44 +00:00
|
|
|
CoordinateType y_min = min_corner.y() + row * cell_height;
|
|
|
|
CoordinateType y_max = min_corner.y() + (row + 1) * cell_height;
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
for (size_t col = 0; col < grid_size; ++col)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
CoordinateType x_min = min_corner.x() + col * cell_width;
|
|
|
|
CoordinateType x_max = min_corner.x() + (col + 1) * cell_width;
|
2019-12-27 05:51:52 +00:00
|
|
|
#pragma GCC diagnostic pop
|
2017-09-20 02:30:44 +00:00
|
|
|
Box cell_box(Point(x_min, y_min), Point(x_max, y_max));
|
|
|
|
|
|
|
|
MultiPolygon intersection;
|
2020-03-25 19:19:49 +00:00
|
|
|
boost::geometry::intersection(polygon, cell_box, intersection);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-03-25 19:50:18 +00:00
|
|
|
size_t cell_index = getCellIndex(row, col);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
if (intersection.empty())
|
2020-03-25 19:50:18 +00:00
|
|
|
addCell(cell_index, cell_box);
|
2017-09-20 02:30:44 +00:00
|
|
|
else if (intersection.size() == 1)
|
2020-03-25 19:50:18 +00:00
|
|
|
addCell(cell_index, cell_box, intersection.front());
|
2017-09-20 02:30:44 +00:00
|
|
|
else if (intersection.size() == 2)
|
2020-03-25 19:50:18 +00:00
|
|
|
addCell(cell_index, cell_box, intersection.front(), intersection.back());
|
2017-09-20 02:30:44 +00:00
|
|
|
else
|
2020-03-25 19:50:18 +00:00
|
|
|
addComplexPolygonCell(cell_index, cell_box);
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
2020-03-25 19:07:10 +00:00
|
|
|
bool PointInPolygonWithGrid<CoordinateType>::contains(CoordinateType x, CoordinateType y) const
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2017-09-23 23:47:43 +00:00
|
|
|
if (has_empty_bound)
|
|
|
|
return false;
|
|
|
|
|
2021-07-09 03:06:21 +00:00
|
|
|
if (!isFinite(x) || !isFinite(y))
|
2020-06-04 00:59:07 +00:00
|
|
|
return false;
|
|
|
|
|
2017-09-20 02:30:44 +00:00
|
|
|
CoordinateType float_row = (y + y_shift) * y_scale;
|
|
|
|
CoordinateType float_col = (x + x_shift) * x_scale;
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
if (float_row < 0 || float_row > grid_size)
|
2017-09-20 02:30:44 +00:00
|
|
|
return false;
|
2017-09-23 23:47:43 +00:00
|
|
|
if (float_col < 0 || float_col > grid_size)
|
2017-09-20 02:30:44 +00:00
|
|
|
return false;
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
int row = std::min<int>(float_row, grid_size - 1);
|
|
|
|
int col = std::min<int>(float_col, grid_size - 1);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
int index = getCellIndex(row, col);
|
|
|
|
const auto & cell = cells[index];
|
|
|
|
|
|
|
|
switch (cell.type)
|
|
|
|
{
|
|
|
|
case CellType::inner:
|
|
|
|
return true;
|
|
|
|
case CellType::outer:
|
|
|
|
return false;
|
|
|
|
case CellType::singleLine:
|
|
|
|
return cell.half_planes[0].contains(x, y);
|
2018-08-21 09:56:01 +00:00
|
|
|
case CellType::pairOfLinesSingleConvexPolygon:
|
2017-09-20 02:30:44 +00:00
|
|
|
return cell.half_planes[0].contains(x, y) && cell.half_planes[1].contains(x, y);
|
2019-01-04 12:10:00 +00:00
|
|
|
case CellType::pairOfLinesDifferentPolygons: [[fallthrough]];
|
2018-08-21 09:56:01 +00:00
|
|
|
case CellType::pairOfLinesSingleNonConvexPolygons:
|
2017-09-20 02:30:44 +00:00
|
|
|
return cell.half_planes[0].contains(x, y) || cell.half_planes[1].contains(x, y);
|
|
|
|
case CellType::complexPolygon:
|
|
|
|
return boost::geometry::within(Point(x, y), polygons[cell.index_of_inner_polygon]);
|
|
|
|
}
|
2019-01-05 03:33:22 +00:00
|
|
|
|
|
|
|
__builtin_unreachable();
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-21 09:56:01 +00:00
|
|
|
template <typename CoordinateType>
|
2019-01-04 12:10:00 +00:00
|
|
|
bool PointInPolygonWithGrid<CoordinateType>::isConvex(const PointInPolygonWithGrid<CoordinateType>::Polygon & poly)
|
2018-08-21 09:56:01 +00:00
|
|
|
{
|
2019-01-04 12:10:00 +00:00
|
|
|
const auto & outer = poly.outer();
|
2018-08-21 09:56:01 +00:00
|
|
|
/// Segment or point.
|
|
|
|
if (outer.size() < 4)
|
|
|
|
return false;
|
|
|
|
|
2020-03-25 20:37:10 +00:00
|
|
|
auto vec_product = [](const Point & from, const Point & to) { return from.x() * to.y() - from.y() * to.x(); };
|
|
|
|
auto get_vector = [](const Point & from, const Point & to) -> Point
|
2018-08-21 09:56:01 +00:00
|
|
|
{
|
|
|
|
return Point(to.x() - from.x(), to.y() - from.y());
|
|
|
|
};
|
|
|
|
|
2020-03-26 07:03:56 +00:00
|
|
|
Point first = get_vector(outer[0], outer[1]);
|
2018-08-21 09:56:01 +00:00
|
|
|
Point prev = first;
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto i : collections::range(1, outer.size() - 1))
|
2018-08-21 09:56:01 +00:00
|
|
|
{
|
2020-03-26 07:03:56 +00:00
|
|
|
Point cur = get_vector(outer[i], outer[i + 1]);
|
2020-03-25 20:37:10 +00:00
|
|
|
if (vec_product(prev, cur) < 0)
|
2018-08-21 09:56:01 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
prev = cur;
|
|
|
|
}
|
|
|
|
|
2020-03-26 07:03:56 +00:00
|
|
|
return vec_product(prev, first) >= 0;
|
2018-08-21 09:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
std::vector<typename PointInPolygonWithGrid<CoordinateType>::HalfPlane>
|
|
|
|
PointInPolygonWithGrid<CoordinateType>::findHalfPlanes(
|
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Box & box,
|
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Polygon & intersection)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
std::vector<HalfPlane> half_planes;
|
|
|
|
const auto & outer = intersection.outer();
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto i : collections::range(0, outer.size() - 1))
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
/// Want to detect is intersection edge was formed from box edge or from polygon edge.
|
2020-03-25 20:37:10 +00:00
|
|
|
/// If section (x1, y1), (x2, y2) is on box edge, then either x1 = x2 = one of box_x or y1 = y2 = one of box_y
|
|
|
|
|
|
|
|
auto x1 = outer[i].x();
|
|
|
|
auto y1 = outer[i].y();
|
|
|
|
auto x2 = outer[i + 1].x();
|
|
|
|
auto y2 = outer[i + 1].y();
|
|
|
|
|
|
|
|
auto box_x1 = box.min_corner().x();
|
|
|
|
auto box_y1 = box.min_corner().y();
|
|
|
|
auto box_x2 = box.max_corner().x();
|
|
|
|
auto box_y2 = box.max_corner().y();
|
|
|
|
|
|
|
|
if (! ((x1 == x2 && (x1 == box_x1 || x2 == box_x2))
|
|
|
|
|| (y1 == y2 && (y1 == box_y1 || y2 == box_y2))))
|
|
|
|
{
|
|
|
|
half_planes.emplace_back(Point(x1, y1), Point(x2, y2));
|
|
|
|
}
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return half_planes;
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
void PointInPolygonWithGrid<CoordinateType>::addComplexPolygonCell(
|
|
|
|
size_t index, const PointInPolygonWithGrid<CoordinateType>::Box & box)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
cells[index].type = CellType::complexPolygon;
|
|
|
|
cells[index].index_of_inner_polygon = polygons.size();
|
|
|
|
|
|
|
|
/// Expand box in (1 + eps_factor) times to eliminate errors for points on box bound.
|
2018-10-10 16:35:09 +00:00
|
|
|
static constexpr CoordinateType eps_factor = 0.01;
|
|
|
|
auto x_eps = eps_factor * (box.max_corner().x() - box.min_corner().x());
|
|
|
|
auto y_eps = eps_factor * (box.max_corner().y() - box.min_corner().y());
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
Point min_corner(box.min_corner().x() - x_eps, box.min_corner().y() - y_eps);
|
|
|
|
Point max_corner(box.max_corner().x() + x_eps, box.max_corner().y() + y_eps);
|
|
|
|
Box box_with_eps_bound(min_corner, max_corner);
|
|
|
|
|
|
|
|
MultiPolygon intersection;
|
2020-03-25 20:06:27 +00:00
|
|
|
boost::geometry::intersection(polygon, box_with_eps_bound, intersection);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
|
|
|
polygons.push_back(intersection);
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
void PointInPolygonWithGrid<CoordinateType>::addCell(
|
|
|
|
size_t index, const PointInPolygonWithGrid<CoordinateType>::Box & empty_box)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
const auto & min_corner = empty_box.min_corner();
|
|
|
|
const auto & max_corner = empty_box.max_corner();
|
|
|
|
|
|
|
|
Point center((min_corner.x() + max_corner.x()) / 2, (min_corner.y() + max_corner.y()) / 2);
|
|
|
|
|
|
|
|
if (boost::geometry::within(center, polygon))
|
|
|
|
cells[index].type = CellType::inner;
|
|
|
|
else
|
|
|
|
cells[index].type = CellType::outer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
void PointInPolygonWithGrid<CoordinateType>::addCell(
|
2017-09-20 02:30:44 +00:00
|
|
|
size_t index,
|
2017-09-23 23:47:43 +00:00
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Box & box,
|
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Polygon & intersection)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
if (!intersection.inners().empty())
|
|
|
|
addComplexPolygonCell(index, box);
|
|
|
|
|
|
|
|
auto half_planes = findHalfPlanes(box, intersection);
|
|
|
|
|
|
|
|
if (half_planes.empty())
|
2020-03-25 20:37:10 +00:00
|
|
|
{
|
2017-09-20 02:30:44 +00:00
|
|
|
addCell(index, box);
|
2020-03-25 20:37:10 +00:00
|
|
|
}
|
2017-09-20 02:30:44 +00:00
|
|
|
else if (half_planes.size() == 1)
|
|
|
|
{
|
|
|
|
cells[index].type = CellType::singleLine;
|
|
|
|
cells[index].half_planes[0] = half_planes[0];
|
|
|
|
}
|
|
|
|
else if (half_planes.size() == 2)
|
|
|
|
{
|
2018-08-21 09:56:01 +00:00
|
|
|
cells[index].type = isConvex(intersection) ? CellType::pairOfLinesSingleConvexPolygon
|
|
|
|
: CellType::pairOfLinesSingleNonConvexPolygons;
|
2017-09-20 02:30:44 +00:00
|
|
|
cells[index].half_planes[0] = half_planes[0];
|
|
|
|
cells[index].half_planes[1] = half_planes[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
addComplexPolygonCell(index, box);
|
|
|
|
}
|
|
|
|
|
2017-09-23 23:47:43 +00:00
|
|
|
template <typename CoordinateType>
|
|
|
|
void PointInPolygonWithGrid<CoordinateType>::addCell(
|
2017-09-20 02:30:44 +00:00
|
|
|
size_t index,
|
2017-09-23 23:47:43 +00:00
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Box & box,
|
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Polygon & first,
|
|
|
|
const PointInPolygonWithGrid<CoordinateType>::Polygon & second)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
|
|
|
if (!first.inners().empty() || !second.inners().empty())
|
|
|
|
addComplexPolygonCell(index, box);
|
|
|
|
|
|
|
|
auto first_half_planes = findHalfPlanes(box, first);
|
|
|
|
auto second_half_planes = findHalfPlanes(box, second);
|
|
|
|
|
|
|
|
if (first_half_planes.empty())
|
|
|
|
addCell(index, box, first);
|
|
|
|
else if (second_half_planes.empty())
|
|
|
|
addCell(index, box, second);
|
|
|
|
else if (first_half_planes.size() == 1 && second_half_planes.size() == 1)
|
|
|
|
{
|
|
|
|
cells[index].type = CellType::pairOfLinesDifferentPolygons;
|
|
|
|
cells[index].half_planes[0] = first_half_planes[0];
|
|
|
|
cells[index].half_planes[1] = second_half_planes[0];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
addComplexPolygonCell(index, box);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Algorithms.
|
|
|
|
|
|
|
|
template <typename T, typename U, typename PointInPolygonImpl>
|
|
|
|
ColumnPtr pointInPolygon(const ColumnVector<T> & x, const ColumnVector<U> & y, PointInPolygonImpl && impl)
|
|
|
|
{
|
|
|
|
auto size = x.size();
|
|
|
|
|
|
|
|
if (impl.hasEmptyBound())
|
2017-12-14 01:43:19 +00:00
|
|
|
return ColumnVector<UInt8>::create(size, 0);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2017-12-14 01:43:19 +00:00
|
|
|
auto result = ColumnVector<UInt8>::create(size);
|
2017-09-20 02:30:44 +00:00
|
|
|
auto & data = result->getData();
|
|
|
|
|
|
|
|
const auto & x_data = x.getData();
|
|
|
|
const auto & y_data = y.getData();
|
|
|
|
|
2021-06-15 19:55:21 +00:00
|
|
|
for (auto i : collections::range(0, size))
|
2017-09-20 02:30:44 +00:00
|
|
|
data[i] = static_cast<UInt8>(impl.contains(x_data[i], y_data[i]));
|
|
|
|
|
Get rid of useless std::move to get NRVO
http://eel.is/c++draft/class.copy.elision#:constructor,copy,elision
Some quote:
> Speaking of RVO, return std::move(w); prohibits it. It means "use move constructor or fail to compile", whereas return w; means "use RVO, and if you can't, use move constructor, and if you can't, use copy constructor, and if you can't, fail to compile."
There is one exception to this rule:
```cpp
Block FilterBlockInputStream::removeFilterIfNeed(Block && block)
{
if (block && remove_filter)
block.erase(static_cast<size_t>(filter_column));
return std::move(block);
}
```
because references are not eligible for NRVO, which is another rule "always move rvalue references and forward universal references" that takes precedence.
2018-08-27 14:04:22 +00:00
|
|
|
return result;
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ... Types>
|
|
|
|
struct CallPointInPolygon;
|
|
|
|
|
|
|
|
template <typename Type, typename ... Types>
|
|
|
|
struct CallPointInPolygon<Type, Types ...>
|
|
|
|
{
|
|
|
|
template <typename T, typename PointInPolygonImpl>
|
|
|
|
static ColumnPtr call(const ColumnVector<T> & x, const IColumn & y, PointInPolygonImpl && impl)
|
|
|
|
{
|
|
|
|
if (auto column = typeid_cast<const ColumnVector<Type> *>(&y))
|
|
|
|
return pointInPolygon(x, *column, impl);
|
|
|
|
return CallPointInPolygon<Types ...>::template call<T>(x, y, impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PointInPolygonImpl>
|
|
|
|
static ColumnPtr call(const IColumn & x, const IColumn & y, PointInPolygonImpl && impl)
|
|
|
|
{
|
2021-11-25 20:55:02 +00:00
|
|
|
using Impl = TypeListChangeRoot<CallPointInPolygon, TypeListIntAndFloat>;
|
2017-09-20 02:30:44 +00:00
|
|
|
if (auto column = typeid_cast<const ColumnVector<Type> *>(&x))
|
|
|
|
return Impl::template call<Type>(*column, y, impl);
|
|
|
|
return CallPointInPolygon<Types ...>::call(x, y, impl);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct CallPointInPolygon<>
|
|
|
|
{
|
|
|
|
template <typename T, typename PointInPolygonImpl>
|
2017-12-02 02:47:12 +00:00
|
|
|
static ColumnPtr call(const ColumnVector<T> &, const IColumn & y, PointInPolygonImpl &&)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2017-12-08 01:34:52 +00:00
|
|
|
throw Exception(std::string("Unknown numeric column type: ") + demangle(typeid(y).name()), ErrorCodes::LOGICAL_ERROR);
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename PointInPolygonImpl>
|
2017-12-02 02:47:12 +00:00
|
|
|
static ColumnPtr call(const IColumn & x, const IColumn &, PointInPolygonImpl &&)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2017-12-08 01:34:52 +00:00
|
|
|
throw Exception(std::string("Unknown numeric column type: ") + demangle(typeid(x).name()), ErrorCodes::LOGICAL_ERROR);
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename PointInPolygonImpl>
|
2020-05-22 19:54:16 +00:00
|
|
|
NO_INLINE ColumnPtr pointInPolygon(const IColumn & x, const IColumn & y, PointInPolygonImpl && impl)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2021-11-25 20:55:02 +00:00
|
|
|
using Impl = TypeListChangeRoot<CallPointInPolygon, TypeListIntAndFloat>;
|
2017-09-20 02:30:44 +00:00
|
|
|
return Impl::call(x, y, impl);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename Polygon>
|
2020-05-22 19:37:56 +00:00
|
|
|
UInt128 sipHash128(Polygon && polygon)
|
2017-09-20 02:30:44 +00:00
|
|
|
{
|
2020-05-22 19:37:56 +00:00
|
|
|
SipHash hash;
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-05-22 19:37:56 +00:00
|
|
|
auto hash_ring = [&hash](const auto & ring)
|
2020-03-25 18:59:11 +00:00
|
|
|
{
|
2020-05-22 19:27:38 +00:00
|
|
|
UInt32 size = ring.size();
|
2020-05-22 19:37:56 +00:00
|
|
|
hash.update(size);
|
|
|
|
hash.update(reinterpret_cast<const char *>(ring.data()), size * sizeof(ring[0]));
|
2020-03-25 18:59:11 +00:00
|
|
|
};
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-05-22 19:37:56 +00:00
|
|
|
hash_ring(polygon.outer());
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-03-25 18:59:11 +00:00
|
|
|
const auto & inners = polygon.inners();
|
2020-05-22 19:37:56 +00:00
|
|
|
hash.update(inners.size());
|
2020-03-25 18:59:11 +00:00
|
|
|
for (auto & inner : inners)
|
2020-05-22 19:37:56 +00:00
|
|
|
hash_ring(inner);
|
2017-09-20 02:30:44 +00:00
|
|
|
|
2020-05-22 19:37:56 +00:00
|
|
|
UInt128 res;
|
2021-01-27 00:54:57 +00:00
|
|
|
hash.get128(res);
|
2020-05-22 19:37:56 +00:00
|
|
|
return res;
|
2017-09-20 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 18:51:48 +00:00
|
|
|
}
|