【问题标题】:Dilating a polygon boost::geometry膨胀多边形 boost::geometry
【发布时间】:2016-08-03 14:46:20
【问题描述】:

多边形几何对象有形态膨胀操作吗?

例如,假设我有一个边长为 1 的正方形,以原点为中心(一个 boost::geometry::model::polygon,点为 (.5, .5), (-.5, .5), (-.5, -.5), (.5, -.5))。如果我以 0.5 的距离/半径“扩张”它,我会得到一个边长为 2 的正方形,仍然以原点为中心。也就是说,多边形的所有边都应沿其法线方向“推出”。

【问题讨论】:

    标签: c++ boost boost-geometry


    【解决方案1】:

    这方面的术语是“缓冲区”,缓冲多边形仅在带有策略的 buffer() 的重载中实现:http://www.boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry/reference/algorithms/buffer/buffer_7_with_strategies.html

    这是一个将三角形扩大 0.1 个单位的示例。

    #include <iostream>
    #include <list>
    
    #include <boost/geometry.hpp>
    #include <boost/geometry/geometries/point_xy.hpp>
    #include <boost/geometry/geometries/polygon.hpp>
    
    using coordinate_type = double;
    using point_type = boost::geometry::model::d2::point_xy<coordinate_type>;
    using polygon_type = boost::geometry::model::polygon<point_type>;
    
    int main()
    {
        // Construct
        polygon_type polygon;
        // Counter clock-wise points don't seem to work (no error, but empty output)
    //    boost::geometry::append(polygon, point_type {0,0});
    //    boost::geometry::append(polygon, point_type {1,0});
    //    boost::geometry::append(polygon, point_type {0,1});
    //    boost::geometry::append(polygon, point_type {0,0});
    
        // Points specified in clockwise order
        boost::geometry::append(polygon, point_type {0,0});
        boost::geometry::append(polygon, point_type {0,1});
        boost::geometry::append(polygon, point_type {1,0});
        boost::geometry::append(polygon, point_type {0,0});
    
        //boost::geometry::buffer(poly, output, .5); // THIS_OPERATION_IS_NOT_OR_NOT_YET_IMPLEMENTED
    
        const double buffer_distance = .1;
        const int points_per_circle = 36;
        boost::geometry::strategy::buffer::distance_symmetric<coordinate_type> distance_strategy(buffer_distance);
        boost::geometry::strategy::buffer::join_round join_strategy(points_per_circle);
        boost::geometry::strategy::buffer::end_round end_strategy(points_per_circle);
        boost::geometry::strategy::buffer::point_circle circle_strategy(points_per_circle);
        boost::geometry::strategy::buffer::side_straight side_strategy;
    
        boost::geometry::model::multi_polygon<polygon_type> input;
        input.push_back(polygon);
    
        boost::geometry::model::multi_polygon<polygon_type> outputMultiPolygon;
    
        boost::geometry::buffer(polygon, outputMultiPolygon,
                    distance_strategy, side_strategy,
                    join_strategy, end_strategy, circle_strategy);
    
        std::cout << outputMultiPolygon.size() << std::endl;
    
        polygon_type outputPolygon = outputMultiPolygon[0];
    
        // Print the points of the result (there are many more than in the input because the corners have been rounded)
        for(unsigned int pointID = 0; pointID < outputPolygon.outer().size(); ++pointID) {
            std::cout << boost::geometry::get<0>(outputPolygon.outer()[pointID]) << " " << boost::geometry::get<1>(outputPolygon.outer()[pointID]) << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

    • re: “CCW 似乎不起作用” 在 boost::polygon 中,反向绕组的多边形实际上是一个“洞”——一个负多边形。扩张器会将孔的边缘向内移动。清理反向绕组多边形有两个原因。首先,正多边形外部的洞被认为是错误的并被清除。其次,如果将 1x1 正方形的所有边向内移动 0.5,则会得到一个面积为零的多边形(孔)。
    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多