c++ - sym_difference(linestring, polygon, vector<linestring>) 不起作用?

标签 c++ boost-geometry

来自 here线性/区域应该工作。但是下面的代码导致编译错误?

#include <iostream>
#include <vector>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>
BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
typedef boost::geometry::model::d2::point_xy<double> geo2dpoint;
typedef boost::geometry::model::polygon<geo2dpoint> polygon;
typedef boost::geometry::model::linestring<geo2dpoint> linestr;

int main()
{
    double points[][2] = {{2.0, 1.3}, {4.1, 3.0}, {5.3, 2.6}, {2.9, 0.7}, {2.0, 1.3}};
    polygon poly;
    boost::geometry::append(poly, points);
    std::cout << boost::geometry::dsv(poly) << std::endl;
    linestr line1;
    boost::geometry::append(line1, std::vector<geo2dpoint>{{2., 1.f}, {5.f, 3.f}});
    std::cout << boost::geometry::dsv(line1) << std::endl;
    std::vector<linestr> intlines;
    boost::geometry::sym_difference(line1, poly, intlines);
    return 0;
}
error: no matching function for call to 'assertion_failed'
    BOOST_MPL_ASSERT_MSG

最佳答案

我不确定,但我认为线串的集合适用于multi_linestringmulti_polygon 输入的情况。

以下编译:

Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/algorithms/sym_difference.hpp>
#include <iostream>
#include <vector>

namespace bg = boost::geometry;

typedef bg::model::d2::point_xy<double>      geo2dpoint;
typedef bg::model::polygon<geo2dpoint>       polygon;
typedef bg::model::linestring<geo2dpoint>    linestr;
typedef bg::model::multi_linestring<linestr> multi_linestr;

int main() {
    polygon poly { { { 2.0, 1.3 }, { 4.1, 3.0 }, { 5.3, 2.6 }, { 2.9, 0.7 }, { 2.0, 1.3 } } };
    multi_linestr line1 { { { 2., 1. }, { 5., 3. } } };

    multi_linestr out;
    bg::sym_difference(line1, poly, out);

    std::cout << bg::dsv(poly) << std::endl;
    std::cout << bg::dsv(line1) << std::endl;
    std::cout << bg::dsv(out) << std::endl;
}

打印

(((2, 1.3), (4.1, 3), (5.3, 2.6), (2.9, 0.7), (2, 1.3)))
(((2, 1), (5, 3)))
(((2, 1), (2.225, 1.15)), ((4.7, 2.8), (5, 3)), ((2.225, 1.15), (4.7, 2.8)))

关于c++ - sym_difference(linestring, polygon, vector<linestring>) 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51448904/

相关文章:

c++ - 如何测试是否可以无错加载 DLL

c++ - Boost Vector 大小构造函数不起作用

c++ - 这是一个错误吗? constexpr 构造函数静默变为非 constexpr

c++ - (编译器错误)为什么 C++ 找不到 .h 文件,即使它在那里?

c++ - 编译 boost::geometry 多边形

c++ - 闭合贝塞尔曲线

c++ - boost 交叉口不起作用

c++ - Boost Geometry:计算 vector 差

boost 多边形序列化

c++ - 我可以在 boost::geometry 的多边形内存储附加信息吗?