c++ - 缩小/扩大带孔多边形的轮廓

标签 c++ boost polygon boost-polygon

我想使用 boost::polygon 扩展/收缩带孔的多边形。所以澄清一点,我有一个单一的数据结构

boost::polygon::polygon_with_holes_data<int> inPoly

其中 inPoly 包含描述矩形轮廓和在该矩形内形成孔的三角形的数据(在下图中,这是左侧的黑色绘图)。

现在我想

a) 展开整个东西,使矩形变大,孔变小(导致下图中的红色多边形)或

b) 缩小它,使矩形变小,孔变大(导致下面的绿色图像)。

Polygon

拐角不一定要是直的,也可以是圆的或“粗糙”的。

我的问题:如何使用 boost::polygon 完成此操作?

谢谢!

最佳答案

我回答了这个Expand polygons with boost::geometry?

是的,您可以教 Boost Geometry 作用于 Boost Polygon 类型:

#include <boost/geometry/geometries/adapted/boost_polygon.hpp>

我想出了一个像你描述的那样的测试多边形:

boost::polygon::polygon_with_holes_data<int> inPoly;
bg::read_wkt("POLYGON ((0 0,0 1000,1000 1000,1000 0,0 0),(100 100,900 100,500 700,100 100))", inPoly);

现在,显然我们不能只在适应的多边形上buffer,我们也不能bg::assignbg::convert直接地。所以,我想出了一个丑陋的解决方法,即转换为 WKT 并返回。然后你可以做缓冲区,并以类似的方式返回。

它不是很优雅,但确实有效:

poly in;
bg::read_wkt(boost::lexical_cast<std::string>(bg::wkt(inPoly)), in);

完整演示

包括 SVG 输出:

Live On Coliru

#include <boost/polygon/polygon.hpp>
#include <boost/polygon/polygon_set_data.hpp>
#include <boost/polygon/polygon_with_holes_data.hpp>

#include <boost/geometry.hpp>
#include <boost/geometry/strategies/buffer.hpp>
#include <boost/geometry/algorithms/buffer.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/adapted/boost_polygon.hpp>
#include <fstream>

namespace bp = boost::polygon;
namespace bg = boost::geometry;
using P = bp::polygon_with_holes_data<int>;
using PS = bp::polygon_set_data<int>;
using coordinate_type = bg::coordinate_type<P>::type;

int main() {
    P inPoly, grow, shrink;
    bg::read_wkt("POLYGON ((0 0,0 1000,1000 1000,1000 0,0 0),(100 100,900 100,500 700,100 100))", inPoly);

    {
        // define our boost geometry types
        namespace bs = bg::strategy::buffer;
        namespace bgm = bg::model;
        using pt = bgm::d2::point_xy<coordinate_type>;
        using poly = bgm::polygon<pt>;
        using mpoly = bgm::multi_polygon<poly>;

        // define our buffering strategies
        using dist = bs::distance_symmetric<coordinate_type>;
        bs::side_straight  side_strategy;
        const int points_per_circle = 12;
        bs::join_round   join_strategy(points_per_circle);
        bs::end_round    end_strategy(points_per_circle);
        bs::point_circle point_strategy(points_per_circle);

        poly in;
        bg::read_wkt(boost::lexical_cast<std::string>(bg::wkt(inPoly)), in);

        for (auto [offset, output_p] : { std::tuple(+15, &grow), std::tuple(-15, &shrink) }) {
            mpoly out;
            bg::buffer(in, out, dist(offset), side_strategy, join_strategy, end_strategy, point_strategy);

            assert(out.size() == 1);
            bg::read_wkt(boost::lexical_cast<std::string>(bg::wkt(out.front())), *output_p);
        }
    }

    {
        std::ofstream svg("output.svg");
        using pt = bg::model::d2::point_xy<coordinate_type>;
        boost::geometry::svg_mapper<pt> mapper(svg, 400, 400);
        mapper.add(inPoly);
        mapper.add(grow);
        mapper.add(shrink);

        mapper.map(inPoly, "fill-opacity:0.3;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
        mapper.map(grow, "fill-opacity:0.05;fill:rgb(255,0,0);stroke:rgb(255,0,0);stroke-width:2");
        mapper.map(shrink, "fill-opacity:0.05;fill:rgb(0,0,255);stroke:rgb(0,0,255);stroke-width:2");
    }
}

output.svg写成:

enter image description here

关于c++ - 缩小/扩大带孔多边形的轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62635255/

相关文章:

c++ - 为什么我的背景在 DirectX 12 Release 构建中通过我的网格闪烁?

c++ - 在 HPUX 上编译 NCURSES src

c++ - 为模板结构中定义的成员结构重载运算符<<

C++ 如何将 ip 地址转换为字节?

包含多边形的 MYSQL 多边形

c++ - 在不给参数值的情况下使用 boost::options

c++ - 使用两个不同版本的boost

c++ - 传递 boost::asio::ip::tcp::socket

elasticsearch - 如何在Elastic搜索中处理自相交的GeoJson多边形

python - 如何从两个给定点创建一条无限线以与 Shapely 中的其他几何对象相交?