如何使用Boost.Geometry检查另一个环中是否包含环?

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

我正在尝试使用Boost.Geometry库确定另一个环是否包含环。

我写了以下代码:

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

using namespace boost::geometry;

int main (int argc, const char * argv[])
{
    typedef model::d2::point_xy<double> P;
    model::ring<P> ring1, ring2;

    read_wkt("polygon((0 0,0 3,3 3,3 0,0 0))", ring1);
    read_wkt("polygon((1 1,1 2,2 2,2 1,1 1))", ring2);

    bool b = within(ring1, ring2);
    std::cout << "Within: " << (b ? "YES" : "NO") << std::endl;

    return 0;
}

但是它没有编译(使用Boost 1.48.0),因为它失败了within的静态断言:

NOT_OR_NOT_YET_IMPLEMENTED_FOR_THIS_GEOMETRY_TYPE

似乎within仅支持检查一个点是否在另一个几何体内(根据documentation)。

我正在考虑将环作为线串处理,然后检查它们是否intersect,如果不是 - 检查环的第一个点是否在另一个环内;但我不知道是否可以避免将每个ring复制到linestring

有没有办法实现within两个环的功能(具有合理的性能)?

c++ algorithm boost boost-geometry
1个回答
3
投票

我最终实现了我在问题中建议的想法:

using namespace boost::geometry;

template <typename Point>
bool within(const model::ring<Point>& ring1, const model::ring<Point>& ring2)
{
    return within(ring1.front(), ring2) &&
           disjoint(model::linestring<Point>(ring1.begin(), ring1.end()),
                    model::linestring<Point>(ring2.begin(), ring2.end()));
}

对我来说似乎足够好,但我仍然很乐意得到一些建议。

© www.soinside.com 2019 - 2024. All rights reserved.