c++ - boost::geometry::model::linestring 与 boost::geometry::model::polygon 的交集

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

我试图找到多边形内部的线串部分。我尝试了 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;
}

最佳答案

看来您必须使用 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;
}

关于c++ - boost::geometry::model::linestring 与 boost::geometry::model::polygon 的交集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38810048/

相关文章:

c++ - 重载键多重集的字符串运算符

c++ - 如何打印char数组的地址

c++ - 显式特化 - template-id 与任何模板声明都不匹配

c++ - 如何为按需构造其值的迭代器实现 operator->?

c++ - 我应该在使用之前检查 boost::shared_ptr 或 std::shared_ptr 吗?

python - 在制作 boost.python helloword 演示时不安全地使用相对 rpath libboost.dylib?

c++ - boost 库构建 - 运行时链接和链接选项之间的区别

c# - 用于 P/Invoke 访问的引脚数据

c++ - 使用 libgit2 获取未推送的提交

c++ - 更改功能区按钮的文本颜色