为什么boost :: geometry :: intersection不正确?

时间:2014-05-28 08:59:32

标签: c++ boost boost-geometry

我为Boost几何交叉函数编写了下一个测试函数

typedef boost::geometry::model::polygon<boost::tuple<int, int> > Polygon;

void test_boost_intersection() {
  Polygon green, blue;
  boost::geometry::read_wkt("POLYGON((0 0,0 9,9 9,9 0,0 0))", green);
  boost::geometry::read_wkt("POLYGON((2 2,2 9,9 9,9 2,2 2))", blue);
  std::deque<Polygon> output;
  boost::geometry::intersection(green, blue, output);
  BOOST_FOREACH(Polygon const& p, output)
  {
    std::cout << boost::geometry::dsv(p) << std::endl;
  }
};

我期望输出结果为:

(((2, 2), (2, 9), (9, 9), (9, 2), (2, 2)))

但我得到了:

((((1, 9), (9, 9), (9, 2), (2, 2), (1, 9))))

我使用Boost 1.54。

如果我改变第一个多边形,则交叉点工作正确。

编辑:当我将多边形的类型更改为

boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> >

它开始正常工作。所以我不能一直使用以前的类型吗?

1 个答案:

答案 0 :(得分:2)

您需要 correct 输入多边形以满足算法前提条件: Live On Coliru 打印

(((2, 9), (9, 9), (9, 2), (2, 2), (2, 9)))
#include <boost/tuple/tuple.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
#include <boost/foreach.hpp>
typedef boost::geometry::model::polygon<boost::tuple<int, int> > Polygon;

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

void test_boost_intersection() {
    Polygon green, blue;
    boost::geometry::read_wkt("POLYGON((0 0,0 9,9 9,9 0,0 0))", green);
    boost::geometry::read_wkt("POLYGON((2 2,2 9,9 9,9 2,2 2))", blue);
    boost::geometry::correct(green);
    boost::geometry::correct(blue);
    std::deque<Polygon> output;
    boost::geometry::intersection(green, blue, output);
    BOOST_FOREACH(Polygon const& p, output)
    {
        std::cout << boost::geometry::dsv(p) << std::endl;
    }
}

int main()
{
    test_boost_intersection();
}