为什么boost :: geometry :: intersection无法正常工作? [英] Why boost::geometry::intersection does not work correct?

查看:374
本文介绍了为什么boost :: geometry :: intersection无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Boost Geometry相交函数编写了下一个测试函数

I wrote next test function for Boost Geometry intersection function

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.

I use Boost 1.54.

如果我要更改第一个多边形,则交点可以正常工作.

If I'd change first polygon, intersection works correct.

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

When I changed type of polygon to

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

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

it started to work correct. So can't I use previous type for all times?

推荐答案

您需要

You need to correct the input polygons to satisfy the algorithm preconditions: Live On Coliru prints

(((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();
}

这篇关于为什么boost :: geometry :: intersection无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆