提高内部点的几何多边形距离

时间:2018-07-10 14:15:53

标签: c++ boost boost-geometry

我正在使用boost::geometry处理一些几何任务。我需要满足两个要求:

  • 处理点->多边形相交(是否在内部)。 boost::geometry::within很好用,所以很好
  • 获取任意点到多边形最近边缘的距离。尽管boost::geometry::distance可以正确处理多边形外部的点,但是似乎将多边形视为实体。因此,多边形内的每个点到多边形的距离显然为0。

我尝试对内部/外部内容进行实验,想知道是否有可能获取多边形的内部和外部点的距离。

4 个答案:

答案 0 :(得分:3)

如果点在多边形内部,则可以使用comparable_distance而不是distance算法来加快代码的速度。您无需为每个分段点对计算确切的距离。使用comparable_distance找到最接近给定点的多边形段,然后使用distance算法计算实际距离。

auto distance = std::numeric_limits<float>::max();
if(boost::geometry::within(pt, mPolygon)) 
{
  Segment nearestSegment;
  boost::geometry::for_each_segment(mPolygon, 
    [&distance, &pt, &nearestSegment](const auto& segment) 
     {
        double cmpDst = boost::geometry::comparable_distance(segment,pt);
        if (cmpDst < distance)
        {
          distance = cmpDst;
          nearestSegment = segment; // UPDATE NEAREST SEGMENT
        }
      });
      // CALCULATE EXACT DST
      distance = boost::geometry::distance(nearestSegment,pt);
} else {
  distance = boost::geometry::distance(pt, mPolygon);
}

答案 1 :(得分:1)

我已决定使用以下方法,到目前为止看来似乎提供了正确的结果:

const TPolygonPoint pt{ x, y };
auto distance = std::numeric_limits<float>::max();

if(boost::geometry::within(pt, mPolygon)) {
    boost::geometry::for_each_segment(mPolygon, [&distance, &pt](const auto& segment) {
        distance = std::min<float>(distance, boost::geometry::distance(segment, pt));
    });
} else {
    distance = boost::geometry::distance(pt, mPolygon);
}

return distance;

如果有人知道更快或更更好的方法,请发表评论:)

答案 2 :(得分:0)

为获得最佳性能,应使用带有boost :: geometry :: index的RTree。创建RTree需要付出一定的代价,但是计算到任何(多)多边形环的点的距离将非常快。示例代码:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/index/rtree.hpp>
#include <iostream>
#include <vector>

int main()
{
    namespace bg = boost::geometry;
    namespace bgi = boost::geometry::index;
    typedef bg::model::point<double, 2, bg::cs::cartesian> point;
    typedef bg::model::polygon<point> polygon;

    point p{ 0, 0 };
    // create some polygon and fill it with data
    polygon poly;
    double a = 0;
    double as = bg::math::two_pi<double>() / 100;
    for (int i = 0; i < 100; ++i, a += as)
    {
        double c = cos(a);
        double s = sin(a);
        poly.outer().push_back(point{10 * c, 10 * s});
        poly.inners().resize(1);
        poly.inners()[0].push_back(point{5 * c, 5 * s});
    }
    // make sure it is valid
    bg::correct(poly);

    // create rtree containing objects of type bg::model::pointing_segment
    typedef bg::segment_iterator<polygon const> segment_iterator;
    typedef std::iterator_traits<segment_iterator>::value_type segment_type;

    bgi::rtree<segment_type, bgi::rstar<4> > rtree(bg::segments_begin(poly),
                                                   bg::segments_end(poly));

    // get 1 nearest segment
    std::vector<segment_type> result;
    rtree.query(bgi::nearest(p, 1), std::back_inserter(result));

    BOOST_ASSERT(!result.empty());

    std::cout << bg::wkt(result[0]) << ", " << bg::distance(p, result[0]) << std::endl;

    return 0;
}

答案 3 :(得分:0)

如果您在与外部边界[Polygon Concept一致的多边形中添加内部边界,则可以直接使用boost :: geometry :: distance。

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

namespace bg = boost::geometry;

int main() {
    typedef bg::model::point<int, 2, bg::cs::cartesian> point_t;
    typedef bg::model::polygon<point_t> polygon_t;

    polygon_t poly1;    

    bg::append (poly1.outer(), point_t (1, -1));
    bg::append (poly1.outer(), point_t (1, 1));
    bg::append (poly1.outer(), point_t (-1, 1));
    bg::append (poly1.outer(), point_t (-1, -1));
    bg::append (poly1.outer(), point_t (1, -1));

    poly1.inners().resize (1);
    bg::append (poly1.inners()[0], point_t (1, -1));
    bg::append (poly1.inners()[0], point_t (1, 1));
    bg::append (poly1.inners()[0], point_t (-1, 1));
    bg::append (poly1.inners()[0], point_t (-1, -1));
    bg::append (poly1.inners()[0], point_t (1, -1));


    point_t myPoint (0, 0);
    std::cout << "Minimal distance: " << bg::distance (poly1, myPoint) << std::endl;
    std::cout << "Is within: " << bg::within (myPoint, poly1) << std::endl;

    return 0;
}

->将返回:

    Minimal distance: 1
    Is within: 0

但是,如果执行此操作,则通过boost :: geometry :: within将严格限制在多边形内部的点视为在多边形“外部”。如果您需要这两种功能,则可以维护两个单独的多边形-一个具有内部边界,另一个不具有内部边界。