gpt4 book ai didi

c++ - Boost::Geometry:如何在 multi_polygon 中连接相交的多边形?

转载 作者:可可西里 更新时间:2023-11-01 15:20:38 27 4
gpt4 key购买 nike

所以我想将所有相互关联的多边形加入一个 multi_polygon 中。怎么办?

我们有这样的图像(一个绿色 multi_polygon)我们想要优化(我们可以看到黄色虚线 - 显然是对 multi_polygon 的每个多边形而不是一般的 multi_polygon 执行的简化结果):

enter image description here

这里是生成这种图像的可编译代码:

#include <iostream>
#include <fstream>
#include <boost/assign.hpp>

#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>

#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>

template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
typedef typename boost::geometry::point_type<Geometry1>::type point_type;
std::ofstream svg(filename.c_str());

boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
mapper.add(a);
mapper.add(b);

mapper.map(a, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2");
mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}


boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > make_point(int x, int y)
{
boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > return_item;
boost::geometry::model::d2::point_xy<double> p1(x, y);
boost::geometry::model::d2::point_xy<double> p2(x-1, y);
boost::geometry::model::d2::point_xy<double> p3(x-1, y-1);
boost::geometry::model::d2::point_xy<double> p4(x, y-1);

boost::geometry::append( return_item, p1);
boost::geometry::append( return_item, p2);
boost::geometry::append( return_item, p3);
boost::geometry::append( return_item, p4);

return return_item;

}

int main()
{
// create a container for joined points structure
boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > output, simpl;

// join points one by one (because one day we would have many=))
output.push_back(make_point(1,1));
boost::geometry::correct(output);

output.push_back(make_point(2,1));
boost::geometry::correct(output);

output.push_back(make_point(3,1));
boost::geometry::correct(output);

output.push_back(make_point(4,1));
boost::geometry::correct(output);

output.push_back(make_point(5,1));
boost::geometry::correct(output);

output.push_back(make_point(2,2));
boost::geometry::correct(output);

output.push_back(make_point(3,2));
boost::geometry::correct(output);

output.push_back(make_point(5,2));
boost::geometry::correct(output);

output.push_back(make_point(5,5));
boost::geometry::correct(output);


// simplify joined structure
boost::geometry::simplify(output, simpl, 0.5);

// create an svg image
create_svg("make_envelope.svg", output, simpl );
}

至少需要 boost 1.47.0 和来自 boost/geometry/extensions/io/svg/ 的 3 个文件

我需要的很简单:如何对相互关联的多边形进行分组?在这种情况下,我们将在我们的 multy_poligon 中获得 2 个多边形,如下所示 - 红色和绿色:

enter image description here

更新:

所以我在 dissolve 上找到了这个信息并创建了使用环创建单元格的示例代码:

#include <iostream>
#include <fstream>
#include <boost/assign.hpp>

//Boost
#include <boost/algorithm/string.hpp>
#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/geometries.hpp>
#include <boost/geometry/multi/geometries/multi_polygon.hpp>
#include <boost/geometry/geometries/adapted/boost_tuple.hpp>

BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)

#include <boost/foreach.hpp>

//Boost Geometry extensions (from trunk)
#include <boost/geometry/extensions/io/svg/svg_mapper.hpp>

template <typename Geometry1, typename Geometry2>
void create_svg(std::string const& filename, Geometry1 const& a, Geometry2 const& b)
{
typedef typename boost::geometry::point_type<Geometry1>::type point_type;
std::ofstream svg(filename.c_str());

boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
mapper.add(a);
mapper.add(b);

mapper.map(a, "fill-rule:nonzero;fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:2;");
mapper.map(b, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:4;stroke-dasharray:1,7;stroke-linecap:round");
}

void make_point(int x, int y, boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > & ring)
{
using namespace boost::assign;

ring +=
boost::geometry::model::d2::point_xy<double>(x-1, y-1),
boost::geometry::model::d2::point_xy<double>(x, y-1),
boost::geometry::model::d2::point_xy<double>(x, y),
boost::geometry::model::d2::point_xy<double>(x-1, y),
boost::geometry::model::d2::point_xy<double>(x-1, y-1);

}

int main()
{
using namespace boost::assign;
boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > ring0, ring1,ring;

boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > outputw;

make_point(1, 1, ring) ;
make_point(2, 1, ring) ;
make_point(3, 1, ring) ;
make_point(4, 1, ring) ;
make_point(5, 1, ring) ;
make_point(2, 2, ring) ;
make_point(3, 2, ring) ;
make_point(5, 2, ring) ;

boost::geometry::model::ring<boost::geometry::model::d2::point_xy<double> > output;
boost::geometry::simplify(ring, output, 1);

// create an svg image
create_svg("make_envelope.svg", ring, output );
}

它返回这样的戒指图片:

enter image description here

如果我们可以使用 dissolve 将其变成 poligon,那将真正解决我的一些问题。但看起来目前我们不能由于这个编译器错误问题描述 here

最佳答案

使用 Qt 怎么样。如果您使用 QPolygonF,您可以调用 unite,它可以满足您的需要。一致后,您可以提取点并将它们放回您的助推器容器中。

如果 qt 不是一个选项,请查看此处提出的算法 http://www.wykobi.com

关于c++ - Boost::Geometry:如何在 multi_polygon 中连接相交的多边形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8050607/

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