Search code examples
c++boost-geometry

sym_difference(linestring, polygon, vector<linestring>) does nto work?


from here the linear/areal should work. But following code causes compiling err?

#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

Solution

  • I'm not sure but I thing the collection of linestrings applies to the situation of a multi_linestring or multi_polygon input.

    The following does compile:

    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;
    }
    

    Prints

    (((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)))