使用Boost库分割几何体
有没有办法使用Boost library对几何体执行拆分操作?我不确定您到底在想什么,但您可以检查Boost.geometry()中实现的算法使用Boost库分割几何体,boost,split,boost-geometry,Boost,Split,Boost Geometry,有没有办法使用Boost library对几何体执行拆分操作?我不确定您到底在想什么,但您可以检查Boost.geometry()中实现的算法 我猜您可以使用intersection()或difference()算法来实现它。您对输入内容和拆分定义不够明确 是否有任何方法可以使用Boost库对几何体执行拆分操作 下面是我尝试用一个快速简单但不一定是最优的示例来回答您的问题,说明如何使用Boost.Geometry构建块来组合拆分几何体的解决方案(多种可能的解决方案之一),即在给定点拆分线串 #i
我猜您可以使用intersection()或difference()算法来实现它。您对输入内容和拆分定义不够明确 是否有任何方法可以使用Boost库对几何体执行拆分操作 下面是我尝试用一个快速简单但不一定是最优的示例来回答您的问题,说明如何使用Boost.Geometry构建块来组合拆分几何体的解决方案(多种可能的解决方案之一),即在给定点拆分线串
#include <boost/geometry.hpp>
#include <algorithm>
#include <iostream>
#include <vector>
namespace bg = boost::geometry;
using point_2d = bg::model::d2::point_xy<double>;
using linestring_2d = bg::model::linestring<point_2d>;
int main()
{
point_2d pt(2.5, 0);
linestring_2d ls({{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}});
auto split_begin = std::find_if(bg::segments_begin(ls), bg::segments_end(ls),
[&pt](auto const& segment) { return bg::intersects(segment, pt); });
linestring_2d line1;
std::for_each(bg::segments_begin(ls), split_begin, [&line1](auto const& s)
{
bg::append(line1, s.first);
});
bg::append(line1, split_begin->first);
bg::append(line1, pt);
linestring_2d line2;
bg::append(line2, pt);
bg::append(line2, split_begin->second);
std::for_each(++split_begin, bg::segments_end(ls), [&line2](auto const& s)
{
bg::append(line2, s.second);
});
std::cout << "line: " << bg::dsv(ls) << std::endl;
std::cout << "split point: " << bg::dsv(pt) << std::endl;
std::cout << "line1: " << bg::dsv(line1) << std::endl;
std::cout << "line2: " << bg::dsv(line2) << std::endl;
}
line: ((0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0))
split point: (2.5, 0)
line1: ((0, 0), (1, 0), (2, 0), (2.5, 0))
line2: ((2.5, 0), (3, 0), (4, 0), (5, 0))