简体   繁体   English

增强几何返回相交和相交的不一致结果

[英]Boost geometry returning inconsistent results for intersects and intersection

In my application I'm using boost geometry to perform mainly intersection and difference calculations.在我的应用程序中,我使用 boost 几何来主要执行交集和差异计算。 Unfortunately I noticed an inconsistency in the results of:不幸的是,我注意到结果不一致:

  • bg::intersects and bg::相交和
  • bg::intersection bg::交集

在此处输入图像描述

What I do is:我要做的是:

  1. Calculate the intersection of polygon1 and polygon2 (= polygon3).计算多边形 1 和多边形 2 的交集(= 多边形 3)。
  2. Remove (bg::difference) this intersection from polygon1 (= poly4)从polygon1(= poly4)中删除(bg::difference)这个交集
  3. The resulting polygon4 should not have any intersection with poly2.生成的 poly4 不应与 poly2 有任何交集。
#include <iostream>
#include <boost/geometry.hpp>

namespace bg = boost::geometry;
using point_t = bg::model::d2::point_xy<double>;
using polygon_t = bg::model::polygon<point_t>;
using mpolygon_t = bg::model::multi_polygon<polygon_t>;

int main()
{
    polygon_t poly1, poly2;
    mpolygon_t poly3, poly4, poly5;

    bg::read_wkt("POLYGON(("
        "12227.0 4967.0000000000009, 12238.0 4967.0000000000009, "
        "12238.0 4813.0000000000009, 12227.0 4813.0000000000009, "
        "12227.0 4967.0000000000009))", poly1);
    bg::read_wkt("POLYGON(("
        "12254.0 4947.0, 12219.0 4982.0, 12219.0 5020.0, 12254.0 5055.0, "
        "12261.0 5055.0, 12263.0 5055.0, 12283.0 5055.0, 12283.0 4947.0, "
        "12263.0 4947.0, 12261.0 4947.0, 12254.0 4947.0))", poly2);

    bg::intersection(poly1, poly2, poly3);
    bg::difference(poly1, poly3[0], poly4);

    // b0 = true, b1 = false
    bool b0 = bg::intersects(poly2, poly4[0]);
    bool b1 = bg::intersection(poly2, poly4, poly5) && (poly5.size() != 0);

    bool b2 = !bg::disjoint(poly2, poly4[0]) && !bg::touches(poly2, poly4[0]);
    bool b3 = bg::overlaps(poly2, poly4[0]) || bg::within(poly4[0], poly2) || bg::within(poly2, poly4[0]);

    std::cout << b0 << b1 << b2 << b3 << std::endl;
    return 1;
}

The weird thing is that bg::intersects returns true while bg::intersection returns an empty intersection.奇怪的是 bg::intersects 返回 true 而 bg::intersection 返回一个空的交集。

Does anyone have an idea why this happens?有谁知道为什么会这样? (Maybe accuracy problem?) and even more interesting: How can I avoid such problems? (也许是准确性问题?)甚至更有趣的是:我怎样才能避免这样的问题?

I tried to avoid "intersects" by using other functions but the results were not helpful.我试图通过使用其他功能来避免“相交”,但结果没有帮助。 Please see calculations for b2 and b3.请参阅 b2 和 b3 的计算。

PS: Unfortunately the example seems cause a crash on coliru. PS:不幸的是,这个例子似乎导致了 coliru 的崩溃。

In fact the return value of the intersection function is unspecified: see实际上intersection function 的返回值是未指定的:见

What does boost::geometry::intersection return boost::geometry::intersection 返回什么

So, you need to look at the intersection itself:因此,您需要查看交叉点本身:

#include <iostream>
#include <boost/geometry.hpp>
#include <boost/logic/tribool.hpp>
#include <boost/logic/tribool_io.hpp>

using boost::tribool;
using boost::indeterminate;

namespace bg = boost::geometry;
using Coord = double;
using point_t = bg::model::d2::point_xy<Coord>;
using polygon_t = bg::model::polygon<point_t>;
using mpolygon_t = bg::model::multi_polygon<polygon_t>;

int main() {
    polygon_t a, b;

    bg::read_wkt("POLYGON(("
        "12227.0 4967.0000000000009, 12238.0 4967.0000000000009, "
        "12238.0 4813.0000000000009, 12227.0 4813.0000000000009, "
        "12227.0 4967.0000000000009))", a);
    bg::read_wkt("POLYGON(("
        "12254.0 4947.0, 12219.0 4982.0, 12219.0 5020.0, 12254.0 5055.0, "
        "12261.0 5055.0, 12263.0 5055.0, 12283.0 5055.0, 12283.0 4947.0, "
        "12263.0 4947.0, 12261.0 4947.0, 12254.0 4947.0))", b);

#define CHECK(expected, expr)                                                  \
    do {                                                                       \
        auto const& actual = (expr);                                           \
        auto ok = ((expected) == actual);                                      \
        if (ok == true) {                                                      \
            std::cout << "PASS";                                               \
        } else if (ok == false) {                                              \
            std::cout << "FAIL";                                               \
        } else {                                                               \
            std::cout << "?";                                                  \
        }                                                                      \
        std::cout << "\t" << #expr << " -> " << std::boolalpha << actual       \
                  << "\n";                                                     \
        if (!(ok == true))                                                               \
            std::cout << "  (expected value was " << (expected) << ")\n";      \
    } while (false)

    mpolygon_t ab_intersect;
    auto dump = [](auto label, auto& geo) {
        std::cout << label << " area " << bg::area(geo) << " " << bg::wkt(geo) << "\n";
    };

    CHECK(true, bg::intersection(a, b, ab_intersect));
    CHECK(1, ab_intersect.size());
    dump("a", a);
    dump("b", b);
    dump("ab_intersect", ab_intersect);

    mpolygon_t a_minus_b, b_minus_a;
    bg::difference(a, ab_intersect, a_minus_b);
    bg::difference(b, ab_intersect, b_minus_a);
    CHECK(1, a_minus_b.size());
    CHECK(1, b_minus_a.size());
    dump("a_minus_b", a_minus_b);
    dump("b_minus_a", b_minus_a);

    auto checks = [dump](auto& reduced, auto& other) {
        CHECK(true, bg::intersects(reduced, other));

        mpolygon_t check;
        bg::intersection(reduced, other, check);

        CHECK(true, check.empty());
        dump("check", check);
    };

    std::cout << "-- reduced a vs b: -------\n";
    checks(a_minus_b, b);
    std::cout << "-- reduced b vs a: -------\n";
    checks(b_minus_a, a);
}

Prints印刷

PASS    bg::intersection(a, b, ab_intersect) -> true
PASS    ab_intersect.size() -> 1
a area 1694 POLYGON((12227 4967,12238 4967,12238 4813,12227 4813,12227 4967))
b area 5687 POLYGON((12254 4947,12219 4982,12219 5020,12254 5055,12261 5055,12263 5055,12283 5055,12283 4947,12263 4947,12261 4947,12254 4947))
ab_intersect area 8 MULTIPOLYGON(((12234 4967,12238 4967,12238 4963,12234 4967)))
PASS    a_minus_b.size() -> 1
PASS    b_minus_a.size() -> 1
a_minus_b area 1686 MULTIPOLYGON(((12234 4967,12238 4963,12238 4813,12227 4813,12227 4967,12234 4967)))
b_minus_a area 5679 MULTIPOLYGON(((12238 4963,12238 4967,12234 4967,12219 4982,12219 5020,12254 5055,12261 5055,12263 5055,12283 5055,12283 4947,12263 4947,12261 4947,12254 4947,12238 4963)))
-- reduced a vs b: -------
PASS    bg::intersects(reduced, other) -> true
PASS    check.empty() -> true
check area 0 MULTIPOLYGON()
-- reduced b vs a: -------
PASS    bg::intersects(reduced, other) -> true
PASS    check.empty() -> true
check area 0 MULTIPOLYGON()

The behavior I mentioned is not a bug but fully correct:我提到的行为不是错误,而是完全正确的:

bg::intersection() returns an empty polygon BUT bg::intersection() 返回一个空多边形但是

if I call it with a linestring as output parameter:如果我用线串作为 output 参数调用它:

using linestring_t = bg::model::linestring<point_t>;
linestring_t int1;
bg::intersection(poly2, poly4, int1);
std::cout << "int1: " << bg::wkt(int1) << std::endl;

it works.有用。

TL;DR The usage and my interpretation of bg::intersects() and bg::intersection() was wrong! TL;DR bg::intersects() 和 bg::intersection() 的用法和我的解释是错误的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM