问题
Is there any way to perform split operation on a geometry using Boost library?
回答1:
I'm not sure what do you have in mind exactly but you could check the algorithms implemented in Boost.Geometry (http://www.boost.org/libs/geometry).
I'm guessing that you could implement it using intersection() or difference() algorithm.
回答2:
You are not specific enough about what is your input and what is your definition of splitting it.
Is there any way to perform split operation on a geometry using Boost library?
Below is my attempt to answer your question with a quick and simple, not necessarily optimal, example of how to use Boost.Geometry building blocks to compose solution (one of many possible) for splitting a geometry, namely, a linestring at given point.
#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;
}
Output:
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))
来源:https://stackoverflow.com/questions/15217356/split-geometry-using-boost-library