gpt4 book ai didi

c++ - 为什么 boost::geometry::intersection 不能正常工作?

转载 作者:太空狗 更新时间:2023-10-29 21:43:00 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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

关于c++ - 为什么 boost::geometry::intersection 不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23907096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com