所以我想在多边形中加入所有相互关联的多边形。怎么办这样的事情?
我们想要优化这样的图像(一个绿色multi_polygon)(我们可以看到黄色虚线 - 显然是在multi_polygon的每个poligon上执行的简化的结果,而不是在multi_polygon上)
这里有可编译的代码来生成这样的图像:
#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/geometry/extensions/io/svg/提升1.47.0和3个文件
我需要的是简单的:如何将相互关联的poligons分组?在这种情况下,我们将在我们的multy_poligon中获得2个poligons,如此处所示 - 红色和绿色:
所以我在dissolve上找到了这个信息,并创建了使用环创建cels的示例代码:
#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 );
}
它返回这样的环形图像:
如果我们可以使用溶解将它变成poligon,这将真正解决我的一些问题。但看起来目前我们不能因为这个编译器错误问题描述here
怎么用Qt。如果您使用QPolygonF,您可以调用unite,它可以完全满足您的需求。在齐声之后,您可以提取点并将它们放回您的增强容器中。
如果qt不是一个选项,请看看这里提出的算法http://www.wykobi.com
您可以使用boost库中的此功能:
void union_(Geometry1 const & geometry1, Geometry2 const & geometry2, Collection & output_collection)
如果它们有交叉点或者将它们放入一个集合(矢量,双端队列等),它需要两个几何并将它们组合在一起(如果没有)。