2014-06-05 37 views
2

我无法使用Boost.Polygon中的get_rectangles(output_container_type& output, const T& polygon_set)方法将曼哈顿(直线)多边形切片为矩形。它似乎不适用于自相交多边形,例如下面的多边形。使用Boost.Polygon切片曼哈顿多边形

这里是我的尝试:

#include <iostream> 
#include <boost/polygon/polygon.hpp> 
#include <vector> 

namespace gtl = boost::polygon; 
using namespace boost::polygon::operators; 

int main() { 
    typedef gtl::polygon_90_with_holes_data<int> Polygon; 
    typedef gtl::polygon_traits<Polygon>::point_type Point; 

    Point pts[] = {gtl::construct<Point>(0, 0), // See the image 
    gtl::construct<Point>(0, 10), 
    gtl::construct<Point>(30, 10), 
    gtl::construct<Point>(30, 20), 
    gtl::construct<Point>(10, 20), 
    gtl::construct<Point>(10, 0)}; 

    Polygon poly; 
    gtl::set_points(poly, pts, pts+6); 

    std::vector< gtl::rectangle_data<int> > rects; 
    get_rectangles(rects, poly); 

    std::cout << rects.size() << " rectangle: \n"; 

    for(std::vector<gtl::rectangle_data<int> >::iterator it = rects.begin(); it !=      
     rects.end(); ++it) { 
      // Print out the corner coordinates 
      std::cout << "x1: "<< gtl::xl(*it) << ", x2: " << gtl::xh(*it) 
      << ", y1: "<< gtl::yl(*it) << ", y2: " << gtl::yh(*it) << std::endl; 
    } 
    return 0; 
} 

而这里的输出:

1 rectangle: 
    x1: 10, x2: 30, y1: 10, y2: 20 

此方法处理为一个不相交的多边形,但在这里它只能产生一个矩形,不是两个。 This似乎表明我正在尝试做的事情应该是可能的。我做错了什么,或者这是可能的?

回答

3

您的代码是“压力测试”多边形代码。严格地说,你的点列表没有定义多边形。理论上,平面区域(特别是多边形形状)仅由形成周边的所谓乔丹(简单)曲线很好地定义。用最简单的术语来说,乔丹(简单)曲线不能交叉。

当相同的形状使用具有8个点的下面的列表约旦(简单)曲线重新定义,

Point pts[] = {gtl::construct<Point>(0, 0), 
gtl::construct<Point>(0, 10), 
gtl::construct<Point>(10, 10),  
gtl::construct<Point>(10, 20), 
gtl::construct<Point>(30, 20), 
gtl::construct<Point>(30, 10), 
gtl::construct<Point>(10, 10), 
gtl::construct<Point>(10, 0)}; 

代码产生正确的结果:

2矩形: X1:0 ,x2:10,y1:0,y2:10 x1:10,x2:30,y1:10,y2:20