2016-08-07 50 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

相关问题