简体   繁体   English

扩展多边形boost :: geometry

[英]Dilating a polygon boost::geometry

Is there a morphological dilation operation for polygon geometry objects? 多边形几何对象有形态学膨胀运算吗?

For example, say I have a square with side length 1 centered at the origin (a boost::geometry::model::polygon with points (.5, .5), (-.5, .5), (-.5, -.5), (.5, -.5) ). 例如,假设我有一个边长为1且以原点为中心的正方形(boost :: geometry :: model :: polygon,其点为(.5,.5),(-。5,.5),(-。 5,-.5),(.5,-.5))。 If I "dilate" it with a distance/radius of .5, I would get a square with side length 2, still centered at the origin. 如果我以0.5的距离/半径“扩张”它,我将得到一个边长为2的正方形,该正方形仍以原点为中心。 That is, all of the edges of the polygon should be "pushed out" along their normal direction. 也就是说,多边形的所有边缘都应沿其法线方向“推出”。

The term for this is "buffer", and buffering polygons is only implemented in the overload of buffer() with strategies: http://www.boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry/reference/algorithms/buffer/buffer_7_with_strategies.html 这个术语是“缓冲区”,缓冲多边形只能通过以下策略使用buffer()的重载来实现: http : //www.boost.org/doc/libs/1_61_0/libs/geometry/doc/html/geometry /reference/algorithms/buffer/buffer_7_with_strategies.html

Here is an example that dilates a triangle by .1 units. 这是将三角形扩大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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM