奇怪的行为将 boost::geometry::within() 用于多边形和环

Posted

技术标签:

【中文标题】奇怪的行为将 boost::geometry::within() 用于多边形和环【英文标题】:Strange behavious use boost::geometry::within() for polygon and ring 【发布时间】:2018-05-03 06:52:37 【问题描述】:

我在 Debian 9 上开发了一个应用程序,并由sudo apt-get install 安装了 boost-1.62.0命令。当我打电话给boost::geometry::with_in(polygon, ring) 时,我发现了一个奇怪的行为。多边形与环相交,但不与 in 相交。代码如下:

#include <iostream>
#include <boost/geometry/algorithms/intersection.hpp>
#include <boost/version.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/ring.hpp>
#include <boost/geometry/geometries/polygon.hpp>

using namespace std;
using namespace boost;
using namespace boost::geometry;

int main()

    using Point = boost::geometry::model::d2::point_xy<double>;
    using Polygon = boost::geometry::model::polygon<Point>;
    using Ring = boost::geometry::model::ring<Point>;

    Ring outer;
    append(outer, Point(-6.46720129179205, 15.61591992211971));
    append(outer, Point(-3.617204647384145, 15.61591992211971));
    append(outer, Point(-3.617204647384145, 7.615919922119708));
    append(outer, Point(-6.46720129179205, 7.615919922119708));
    append(outer, Point(-6.46720129179205, 15.61591992211971));

    Polygon polygon;
    polygon.outer() = outer;
    correct(polygon);

    Ring ring;
    append(ring, Point(-3.542202969588097, 7.615919922119709));
    append(ring, Point(-3.542202969588097, 6.81591992211971));
    append(ring, Point(-3.542202969588097, 6.015919922119708));
    append(ring, Point(-3.542202969588097, 5.215919922119708));
    append(ring, Point(-3.542202969588097, 4.415919922119708));
    append(ring, Point(-3.542202969588097, 3.615919922119708));
    append(ring, Point(-3.542202969588097, 2.815919922119709));
    append(ring, Point(-3.542202969588097, 2.015919922119709));
    append(ring, Point(-3.542202969588097, 1.215919922119709));
    append(ring, Point(-3.542202969588097, 0.4159199221197081));
    append(ring, Point(-3.542202969588097, -0.3840800778802915));
    append(ring, Point(-6.542202969588097, -0.3840800778802916));
    append(ring, Point(-6.542202969588097, 0.415919922119708));
    append(ring, Point(-6.542202969588097, 1.215919922119709));
    append(ring, Point(-6.542202969588097, 2.015919922119709));
    append(ring, Point(-6.542202969588097, 2.815919922119709));
    append(ring, Point(-6.542202969588097, 3.615919922119708));
    append(ring, Point(-6.542202969588096, 4.41591992211971));
    append(ring, Point(-6.542202969588097, 5.215919922119709));
    append(ring, Point(-6.542202969588097, 6.015919922119709));
    append(ring, Point(-6.542202969588097, 6.815919922119708));
    append(ring, Point(-6.542202969588097, 7.615919922119708));
    append(ring, Point(-3.542202969588097, 7.615919922119709));
    correct(ring);

    cout << boolalpha
         << within(polygon, ring) << endl /* should return false */
         << covered_by(polygon, ring) << endl; /* should retrurn false */

    return 0;

构建并运行:

 g++ -g -o wi wi.cpp
 ./wi

得到结果:

 true
 true

多边形不在环内,而是boost::geometry::within(polygon, ring)返回true

那么是 boost-1.62 的错误吗?以及如何解决它。

【问题讨论】:

对于 1.58 也是如此。你确定你的计算是正确的吗? 这很奇怪。多边形不在环内。 对于未来,如果您提供一张图片来显示您想要的行为是什么,将会有所帮助。我现在必须用 excel 来做,我可以看到,它们不相交 不同(更简单的)多边形和点坐标是否会发生相同的行为?如果可以简化问题,将对调试有很大帮助。 环是一个带有点列表的矩形:Point(-3.542202969588097, 7.615919922119709), Point(-3.542202969588097, -0.3840800778802915), Point(-6.542202969588097, -0.3840800778802916), Point(-6.542202969588097, 7.615919922119708), Point(-3.542202969588097, 7.615919922119709),其中不包含多边形。 【参考方案1】:

7.615919922119709 - 7.615919922119708 的差是 1×10^-15。

这意味着几何图形垂直重叠。根据标准的简单功能规范,这意味着它们满足within

Create solid polygon in boost geometry

【讨论】:

感谢您的热情回复!您能否更清楚地解释为什么满足within 以及我该如何解决?直观地说,多边形不在环内。谢谢。

以上是关于奇怪的行为将 boost::geometry::within() 用于多边形和环的主要内容,如果未能解决你的问题,请参考以下文章

使用 C 的 getline() 时的奇怪行为

将枚举转换为字符串时的奇怪行为

将全局变量添加到另一个文件中时的奇怪行为

将文件保存到 Azure Blob 存储的奇怪行为

奇怪的 PowerShell 递归行为

将相同的视图控制器推入堆栈有奇怪的行为