代码之家  ›  专栏  ›  技术社区  ›  Wang

sym_difference(linestring,polygon,vector<linestring>)是否有效?

  •  0
  • Wang  · 技术社区  · 8 年前

    从 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
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   sehe    8 年前

    我不确定,但我觉得 收集 适用于 multi_linestring 或 multi_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)))