在增强几何体中创建实心多边形

Create solid polygon in boost geometry

我是 boost geometry 的新手,我用 boost::geometry::assign_points() 创建了多边形。但我只创建该多边形的外部和内部是空的。所以我尝试用两个多边形 A、B 和 A 在 B 内测试 boost::geometry::overlaps(),结果不重叠。

那么,如何创建实心多边形(只知道多边形外点和多边形内点有效)?

根据定义多边形是实心的,直到您减去内环。来自标准的 §6.1.11.1:

A Polygon is a planar Surface defined by 1 exterior boundary and 0 or more interior boundaries. Each interior boundary defines a hole in the Polygon. A Triangle is a polygon with 3 distinct, non-collinear vertices and no interior boundary. ¹

重叠并不代表您认为的意思。

来自 §6.1.15.3(基于 DE-9IM 的命名空间关系谓词)

  • 交叉
  • 之内
  • 重叠

    定义为

    a.Overlaps(b) ⇔ ( dim(I(a)) = dim(I(b)) = dim(I(a) ∩ I(b)))
                     ∧ (a ∩ b ≠ a) ∧ (a ∩ b ≠ b)
    
  • 包含

    a.Contains(b) ⇔ b.Within(a)
    
  • 相交

    a.Intersects(b) ⇔ ! a.Disjoint(b) 
    

在您的情况下,您可能正在寻找 !disjointwithincontainsintersection:

Live On Coliru

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

namespace bg = boost::geometry;

template <typename Geo> void debug(std::string name, Geo const& g) {
    std::string reason;
    std::cout << name << ": " << bg::dsv(g) << " " << bg::is_valid(g, reason) << ", '" << reason << "'\n"; 
}

template <typename Geo, typename F>
void both_ways(std::string name, Geo const& a, Geo const& b, F f) {
    std::cout << name << "(a, b) -> " << f(a,b) << "\n";
    std::cout << name << "(b, a) -> " << f(b,a) << "\n";
}

int main() {
    std::cout << std::boolalpha;

    using Pt = bg::model::d2::point_xy<int>;
    using Poly = bg::model::polygon<Pt>;
    using Multi = bg::model::multi_polygon<Poly>;

    Poly const a {{ { 0,0 }, { 0,3 }, { 3,3 }, { 3,0 }, { 0,0 }} };
    Poly const b {{ { 1,1 }, { 1,2 }, { 2,2 }, { 2,1 }, { 1,1 }} };

    debug("a", a);
    debug("b", b);

#define TEST(algo) both_ways(#algo, a, b, [](auto& a, auto& b) { return bg::algo(a, b); })
    TEST(overlaps);
    TEST(intersects);
    TEST(within);
    //TEST(contains); // contains(a,b) ⇔ within(b,a)
    //TEST(crosses); // not implemented for polygons
    TEST(disjoint);

    both_ways("intersection", a, b, [](auto& a, auto& b) {
        Multi c; 
        bg::intersection(a, b, c);
        return boost::lexical_cast<std::string>(bg::dsv(c));
    });
}

打印

a: (((0, 0), (0, 3), (3, 3), (3, 0), (0, 0))) true, 'Geometry is valid'
b: (((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))) true, 'Geometry is valid'
overlaps(a, b) -> false
overlaps(b, a) -> false
intersects(a, b) -> true
intersects(b, a) -> true
within(a, b) -> false
within(b, a) -> true
disjoint(a, b) -> false
disjoint(b, a) -> false
intersection(a, b) -> ((((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))))
intersection(b, a) -> ((((1, 1), (1, 2), (2, 2), (2, 1), (1, 1))))

¹ OGC Simple Feature / 通用架构