簡體   English   中英

boost::geometry::model::linestring 與 boost::geometry::model::polygon 的交集

[英]Intersection of boost::geometry::model::linestring with boost::geometry::model::polygon

我試圖找到多邊形內部的線串部分。 我嘗試了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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM