使用boost几何缓冲区缩放多边形时的冗余顶点

问题描述 投票:0回答:1

我正在使用boost几何来管理一些多边形,我需要按给定的数量扩展和缩小它们。我正在使用boost :: geometry :: buffer来实现这一点,我想知道是否有更好的选择。我担心的是,如果我展开一个矩形,我最终会得到一个带有12个顶点的多边形(其中8个是不相关的)。这是一些示例代码:

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

using point_t = boost::geometry::model::d2::point_xy<double>;
using polygon_t = boost::geometry::model::polygon<point_t>;
using mpolygon_t = boost::geometry::model::multi_polygon<polygon_t>;

mpolygon_t expand_polygon(const polygon_t& polygon,
                          const double distance) {
    mpolygon_t scaled_polygon;
    // DistanceStrategy is symmetric; want straight lines (no curves)
    boost::geometry::strategy::buffer::distance_symmetric<double>
      distance_strategy{distance};

    // SideStrategy is straight; grow equally on all sides
    boost::geometry::strategy::buffer::side_straight side_strategy;

    // JoinStrategy is miter; 1.0 as limit; Sharp (not rounded) joins
    boost::geometry::strategy::buffer::join_miter join_strategy;

    // EndStrategy is flat; flat (not rounded) ends
    boost::geometry::strategy::buffer::end_flat end_strategy;

    // PointStrategy is square; squares, not circles, if poly is just a point
    boost::geometry::strategy::buffer::point_square point_strategy;

    boost::geometry::buffer(polygon, scaled_polygon,
                            distance_strategy,
                            side_strategy,
                            join_strategy,
                            end_strategy,
                            point_strategy);

    // return scaled polygon offset by supplied distance
    return scaled_polygon;
}

int main() {
    using boost::geometry::get;
    polygon_t rect;
    boost::geometry::read_wkt("POLYGON((5 5,5 8,8 8,8 5, 5 5))", rect);
    mpolygon_t expanded_mpoly = expand_polygon(rect, 1.0);
    auto list_coordinates = [](const point_t& pt) {
                                std::cout.precision(std::numeric_limits<double>::max_digits10);
                                std::cout << "vertex(" << get<0>(pt)
                                          << ", " << get<1>(pt) << ")" << std::endl;
                            };
    boost::geometry::for_each_point(expanded_mpoly, list_coordinates);

    return 0;
}

这是输出:

vertex(4, 5)
vertex(4, 8)
vertex(4, 9)
vertex(5, 9)
vertex(8, 9)
vertex(9, 9)
vertex(9, 8)
vertex(9, 5)
vertex(9, 4)
vertex(8, 4)
vertex(5, 4)
vertex(4, 4)
vertex(4, 5)

我需要提供一个战略论点吗?我应该关注其他一些提升的提示?

c++ boost buffer polygon boost-geometry
1个回答
1
投票

Boost包含simplify算法(它使用Ramer–Douglas–Peucker algorithm)。例如,在你的多边形中有3个点p1,p2,p3,我们计算点p2和线|p1p3|之间的距离,如果这个距离小于给定的epsilon值,则从多边形中移除p2点。因此,如果使用具有非常小的epsilon值的此函数(要删除共线点),则可以从多边形的所有边删除所有冗余点。试试这个

mpolygon_t out; // to hold new geometry
boost::geometry::simplify(expanded_mpoly,out,0.00001); // very small epsilon value
auto list_coordinates = [](const point_t& pt) {
                            std::cout.precision(std::numeric_limits<double>::max_digits10);
                            std::cout << "vertex(" << get<0>(pt)
                                      << ", " << get<1>(pt) << ")" << std::endl;
                        };
boost::geometry::for_each_point(out, list_coordinates); // print out

作为输出,你看

vertex(9, 9)
vertex(9, 4)
vertex(4, 4)
vertex(4, 9)
vertex(9, 9)
© www.soinside.com 2019 - 2024. All rights reserved.