使用boost :: geometry :: difference,为什么在使用multi_polygon而不是正多边形时我得到一个空结果?

时间:2015-01-14 23:26:11

标签: c++ boost boost-geometry

给出以下代码:

typedef boost::geometry::model::d2::point_xy<int16_t, boost::geometry::cs::cartesian> Point_t;
typedef boost::geometry::model::polygon<Point_t> Polygon_t;
typedef boost::geometry::model::multi_polygon<Polygon_t> MultiPolygon_t;

std::vector<Point_t> points1;
points1.push_back(Point_t(1473, 627));
points1.push_back(Point_t(1473, 1155));
points1.push_back(Point_t(908, 1155));
points1.push_back(Point_t(908, 627));

Polygon_t poly1;
boost::geometry::assign_points(poly1, points1);
boost::geometry::correct(poly1);

MultiPolygon_t multiPoly;

multiPoly.push_back(poly1);

std::vector<Point_t> points2;
points2.push_back(Point_t(1956, 956));
points2.push_back(Point_t(1956, 1028));
points2.push_back(Point_t(115, 1023));
points2.push_back(Point_t(127, 951));

Polygon_t poly2;
boost::geometry::assign_points(poly2, points2);
boost::geometry::correct(poly2);

MultiPolygon_t resultMulti;
MultiPolygon_t resultSimple;

boost::geometry::difference(multiPoly, poly2, resultMulti);
boost::geometry::difference(poly1, poly2, resultSimple);

bool bMultiEmpty = resultMulti.empty();
bool bSimpleEmpty = resultSimple.empty();

EAGLE_ASSERT(!bSimpleEmpty);
EAGLE_ASSERT(!bMultiEmpty);

我得到了结果:

bSimpleEmpty -> FALSE
bMultiEmpty -> TRUE

我希望在两种情况下结果都是非空的...多边形中唯一的东西是同一个多边形用于bSimpleEmpty计算。我是否陷入了误解?

使用Boost 1.51

请指教!

2 个答案:

答案 0 :(得分:1)

当使用16位整数作为坐标类型的模板参数时,看起来问题是boost库中的溢出。使用双打时似乎完美无缺。

答案 1 :(得分:0)

我没有&#34;断开&#34;。您确定要使用相同的代码吗?如果是这样,请查看升级Boost是否有帮助:

<强> Live On Coliru

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

typedef boost::geometry::model::d2::point_xy<int16_t, boost::geometry::cs::cartesian> Point_t;
typedef boost::geometry::model::polygon<Point_t> Polygon_t;
typedef boost::geometry::model::multi_polygon<Polygon_t> MultiPolygon_t;

int main() {

    Polygon_t poly1;
    {
        std::vector<Point_t> points1;
        points1.push_back(Point_t(1473, 627));
        points1.push_back(Point_t(1473, 1155));
        points1.push_back(Point_t(908,  1155));
        points1.push_back(Point_t(908,  627));

        boost::geometry::assign_points(poly1, points1);
        boost::geometry::correct(poly1);
    }

    Polygon_t poly2;
    {
        std::vector<Point_t> points2;
        points2.push_back(Point_t(1956, 956));
        points2.push_back(Point_t(1956, 1028));
        points2.push_back(Point_t(115,  1023));
        points2.push_back(Point_t(127,  951));

        boost::geometry::assign_points(poly2, points2);
        boost::geometry::correct(poly2);
    }

    //////////////////////////////////
    //
    MultiPolygon_t multiPoly;
    multiPoly.push_back(poly1);

    MultiPolygon_t resultMulti;
    MultiPolygon_t resultSimple;

    boost::geometry::difference(multiPoly, poly2, resultMulti);
    boost::geometry::difference(poly1,     poly2, resultSimple);

    bool bMultiEmpty  = resultMulti.empty();
    bool bSimpleEmpty = resultSimple.empty();

    std::cout << std::boolalpha << bSimpleEmpty << ", " << bMultiEmpty << "\n";
}

打印

true, true