【问题标题】:Intersection of boost::geometry::model::linestring with boost::geometry::model::polygonboost::geometry::model::linestring 与 boost::geometry::model::polygon 的交集
【发布时间】:2016-08-07 01:16:18
【问题描述】:

我试图找到多边形内部的线串部分。我尝试了intersection 函数,但它似乎只是找到了实际的交点,而不是与多边形重叠的线串部分。有没有办法得到这个对象?

这是一个演示情况:

#include <iostream>
#include <fstream>

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/svg/svg_mapper.hpp>
#include <boost/geometry/geometries/linestring.hpp>

using point_type = boost::geometry::model::d2::point_xy<double>;
using polygon_type = boost::geometry::model::polygon<point_type>;
using linestring_type = boost::geometry::model::linestring<point_type>;

int main()
{
    polygon_type polygon;

    polygon.outer().push_back(point_type{10,10});
    polygon.outer().push_back(point_type{12,10});
    polygon.outer().push_back(point_type{12,12});
    polygon.outer().push_back(point_type{10,12});
    polygon.outer().push_back(point_type{10,10});

    linestring_type linestring;
    linestring.push_back(point_type{11,9});
    linestring.push_back(point_type{11,11});
    linestring.push_back(point_type{13,11});

    // Expected intersections at (11, 10) and (12, 11)

    std::ofstream svg("both.svg");

    linestring_type output;
    boost::geometry::intersection(polygon, linestring, output);

    for(auto iter = output.begin(); iter != output.end(); ++iter) {
        std::cout << boost::geometry::get<0>(*iter) << " " << boost::geometry::get<1>(*iter) << std::endl;
    }
// The output is:
//    11 10
//    12 11

// But I want it to be:
//    11 10
//    11 11
//    12 11
    return 0;
}

【问题讨论】:

    标签: c++ c++11 boost boost-geometry


    【解决方案1】:

    看来您必须使用 multi_linestring 作为输出类型:

    #include <iostream>
    
    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    #include <boost/geometry/geometries/linestring.hpp>
    #include <boost/geometry/multi/geometries/multi_linestring.hpp>
    
    using point_type = boost::geometry::model::d2::point_xy<double>;
    using polygon_type = boost::geometry::model::polygon<point_type>;
    using linestring_type = boost::geometry::model::linestring<point_type>;
    using multi_linestring_type = boost::geometry::model::multi_linestring<linestring_type>;
    
    int main()
    {
        polygon_type polygon;
    
        polygon.outer().push_back(point_type{10,10});
        polygon.outer().push_back(point_type{10,12});
        polygon.outer().push_back(point_type{12,12});
        polygon.outer().push_back(point_type{12,10});
        polygon.outer().push_back(point_type{10,10});
    
        linestring_type linestring;
        linestring.push_back(point_type{11,9});
        linestring.push_back(point_type{11,11});
        linestring.push_back(point_type{13,11});
    
        // Expected intersections at (11, 10) and (12, 11)
    
        multi_linestring_type intersection;
        boost::geometry::intersection(polygon, linestring, intersection);
    
        for(auto intersectionIter = intersection.begin(); intersectionIter != intersection.end(); ++intersectionIter) {
            linestring_type intersectionPiece = *intersectionIter;
            std::cout << "Piece:" << std::endl;
            for(auto intersectionPieceIter = intersectionPiece.begin(); intersectionPieceIter != intersectionPiece.end(); ++intersectionPieceIter) {
                std::cout << boost::geometry::get<0>(*intersectionPieceIter) << " " << boost::geometry::get<1>(*intersectionPieceIter) << std::endl;
            }
    
        }
    
        return 0;
    }
    

    【讨论】:

    • 它不会那样“出现”。您基本上是在此处引用文档。
    • @sehe 从boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry/… 来看,考虑到可能的输入类型的大量组合,应该期望什么类型并不明显。我尝试的第一种方法,我想在这种情况下生成线串作为输出时,我会期待正确的答案,因为它不是不相交的(多)线串。无论如何,对于那些想要这样做的人来说,这个例子现在在这里:)
    • 来自该链接:"GeometryOut 和几何集合:(例如 std::vector、std::deque、boost::geometry::multi*),其中 value_type 满足 Point , LineString 或 Polygon 概念,或者它是输出几何体(例如对于一个盒子)” 我真的很想知道可以添加什么信息。此外,该示例已经在同一个文档页面上。
    猜你喜欢
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多