Why boost::geometry::intersection does not work correct?

天大地大妈咪最大 提交于 2019-12-12 14:24:26

问题


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

I expected output result as:

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

but I got:

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

I use Boost 1.54.

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

EDIT: 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?


回答1:


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


来源:https://stackoverflow.com/questions/23907096/why-boostgeometryintersection-does-not-work-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!