简体   繁体   English

boost :: geometry :: Polygon和“direct”Polyline的交集

[英]boost::geometry::intersection of Polygon and “directed” Polyline

I need to find all intersection points of polyline and polygon such that polyline intersects polygon from outside. 我需要找到折线和多边形的所有交点,使折线与外部的多边形相交。 I marked such points with bold dots on an attached picture. 我用附加图片上的粗点标记了这些点。

The problem is that a boost::geometry::intersection returns all intersection points and I need to check the intersection type somehow. 问题是boost::geometry::intersection返回所有交叉点,我需要以某种方式检查交集类型。

Here is some small example of using boost::geometry::intersection : 以下是使用boost::geometry::intersection一些小例子:

BOOST_GEOMETRY_REGISTER_POINT_2D(Eigen::Vector2d, double, cs::cartesian, x(), y())
BOOST_GEOMETRY_REGISTER_POINT_3D(Eigen::Vector3d, double, cs::cartesian, x(), y(), z())
BOOST_GEOMETRY_REGISTER_LINESTRING(std::vector<Eigen::Vector2d>)
BOOST_GEOMETRY_REGISTER_MULTI_LINESTRING(std::vector<std::vector<Eigen::Vector2d>>)

using Point = Eigen::Vector2d;
using Polyline = std::vector<Point>;
using Polygon = boost::geometry::model::polygon<Point>;

Polyline polyline{{0, -10}, {0, 10}};

Polygon polygon;
std::vector<Point> polygon_points{{-1., 1.}, {1., 1.}, {1, -1}, {-1, -1}, {-1, 1}};
boost::geometry::assign_points(polygon, polygon_points);

std::vector<Point> intersection_points;

boost::geometry::intersection(polygon, polyline, intersection_points);

for (const auto& p : intersection_points) {
  std::cout << p << std::endl << std::endl; //  here we have 2 points, but I need only one (0, -1)
}

在此输入图像描述

This is how I'd do it: 我就是这样做的:

Do for each segment of the polyline: 对折线的每个部分做:

  1. get the intersection 's of this segment only and the polygon; 得到这个段的intersection和多边形; (If none then continue ). (如果没有则continue )。

  2. use within to check if the start terminal is inside or outside the polygon; 使用within ,以检查是否启动端子是内部或外部的多边形;

  3. if inside, then append every intersection with an odd-numbered index to the output; 如果在里面,则将每个交叉点附加一个奇数编号的索引到输出;

  4. if outside, then append every intersection with an even-numbered index (and zero) to the output. 如果在外面,则将每个交叉点附加一个偶数索引(和零)到输出。

Done. 完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM