c++ - 在 boost 几何体中创建实心多边形

标签 c++ boost geometry 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 的命名空间关系谓词)

  • 十字架 enter image description here
  • enter image description here
  • 重叠 enter image description here

    定义为

    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/通用架构

关于c++ - 在 boost 几何体中创建实心多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47312330/

相关文章:

c++ - QtCreator : breakpoints not working (in debug mode)

c++ - 打印 boost 属性树生成的 xml

c++ - 如何在 pkg-config 中使用 C++ Boost 库?

java - 使用 Java 或 PostgreSQL/PostGIS 将路径分为 N 段

C++(函数、余弦)没有给出正确答案

c++ - 如何检查一个 float 是否是 C++ 中的真实数字?

c++ - 如何在 C++ 中仅用一个扬声器播放声音?

c++ - 具有优先队列的 BGL DFS 访问者

C++ 如何在可移植代码中处理 tr1 和非 tr1 命名空间?

javascript - 使用draw2d在JavaScript中围绕主圆绘制圆