【发布时间】:2016-05-09 09:26:38
【问题描述】:
我需要在多边形中找到自交点。 我知道boost有这种能力。 但我不知道如何使用 turn_info 来获取有关十字路口的信息。比如哪些段相交等等。 谁能帮忙? 谢谢
【问题讨论】:
标签: boost polygon computational-geometry boost-geometry set-intersection
我需要在多边形中找到自交点。 我知道boost有这种能力。 但我不知道如何使用 turn_info 来获取有关十字路口的信息。比如哪些段相交等等。 谁能帮忙? 谢谢
【问题讨论】:
标签: boost polygon computational-geometry boost-geometry set-intersection
您不能,因为 Boost Geometry 定义的概念不允许自相交。
但是,您可以间接使用验证功能(我认为是 1.59 以来的新功能)来获取有关自交集的一些信息:
std::string reason;
poly p;
bg::read_wkt("POLYGON((0 0, 0 4, 2 4, 2 2, 6 2, 6 6, 2 6, 2 4, 0 4, 0 8, 8 8, 8 0, 0 0))", expected);
bool ok = bg::is_valid(p, reason);
std::cout << "Expected: " << bg::dsv(p) << (ok?" valid":" invalid: '" + reason + "'") << "\n";
打印:
预期:(((0, 0), (0, 4), (2, 4), (2, 2), (6, 2), (6, 6), (2, 6), ( 2, 4), (0, 4), (0, 8), (8, 8), (8, 0), (0, 0))) invalid: '几何有无效的自相交。在 (0, 4) 处发现了一个自交点;方法:t;操作:x/u;分段 ID {source, multi, ring, segment}:{0, -1, -1, 0}/{0, -1, -1, 7}'
【讨论】:
这是获取自交点的代码。
namespace bg = boost::geometry;
using namespace std;
typedef bg::model::d2::point_xy<double> point_2d;
typedef bg::model::polygon<boost::geometry::model::d2::point_xy<double> > Polygon;
Polygon poly { { { 10, 10 }, { 20, 10 }, { 20, 5 }, { 25, 5 }, { 25, 7 }, { 30, 7 }, { 30, 3 }, { 25, 3 }, { 25, 5 }, { 20, 5 }, { 20, 0 }, { 10, 0 }, { 10, 10 } }};
typedef bg::point_type<Polygon>::type point_type;
typedef boost::geometry::detail::overlay::turn_info<point_type, boost::geometry::segment_ratio<double> > TurnInfoType;
bg::detail::no_rescale_policy robust_policy;
bg::detail::self_get_turn_points::no_interrupt_policy interrupt_policy;
std::vector<TurnInfoType> turns;
boost::geometry::self_turns<boost::geometry::detail::overlay::assign_null_policy>(poly.outer(), robust_policy, turns, interrupt_policy);
要获取信息,只需使用以下内容:
turns[i].operations[0].seg_id.segment_index
【讨论】: