2016-08-07 17 views
1

我試圖找到一個多邊形內的線串的部分。我嘗試了intersection函數,但它似乎只找到實際的交點,而不是與多邊形重疊的線串部分。有什麼辦法來獲得這個對象?boost :: geometry :: model :: linear :: boost :: geometry :: intersector :: geometry :: model :: polygon

這裏是一個演示情況:

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

回答

2

看來,你必須使用一個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; 
} 
+0

它沒有「出現」這樣的。你基本上是在這裏引用文檔。 – sehe

+0

@sehe從http://www.boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry/reference/algorithms/intersection.html中看不出什麼類型,因爲噸可能的輸入類型的組合。我嘗試的第一種方法是,我想在這種情況下生成一個線串作爲輸出時,我會預期得到正確的答案,因爲它不是不相交的(多)線串。無論如何,這個例子現在在這裏爲那些想要這樣做:) –

+0

從那個鏈接:_「GeometryOut& 幾何的集合:(例如std :: vector,std :: deque,boost :: geometry :: multi * )其中的value_type實現了Point,LineString或Polygon概念,或者它是輸出幾何體(例如對於一個框)「_我真的不知道可以添加什麼信息。另外,該示例已經在那個非常相同的文檔頁面上。 – sehe

相關問題