c – Boost :: Geometry:如何在multi_polygon中加入相交的多边形?

前端之家收集整理的这篇文章主要介绍了c – Boost :: Geometry:如何在multi_polygon中加入相交的多边形?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我想加入所有相互关联的poligons在一个multi_polygon.怎么做这样的事情?

我们有这样的图像(一个绿色多分支),我们想要优化(我们可以看到黄色点线 – 简化的结果,显然是在multi_polygon的每个poligon上执行的,而不是在多分支上)

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

#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,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,2));
    boost::geometry::correct(output);

    output.push_back(make_point(3,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?在这种情况下,我们会在这里显示如下的红色和绿色的两个poligons:

更新:

所以我在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,"fill-rule:nonzero;fill-opacity:0.5;fill:rgb(153,0);stroke-width:2;");
    mapper.map(b,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),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,ring) ;
    make_point(3,ring) ;
    make_point(4,ring) ;
    make_point(5,2,ring) ;

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

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

它返回这样的环形图像:

如果我们可以使用溶解剂将其转化成poligon,这将真正解决我的一些问题.但是看起来像现在我们不能因为这个编译错误的问题描述了here

解决方法

如何使用Qt.如果您使用QPolygonF,您可以调用unite,它完全符合您所需要的.一致后,您可以提取积分并将其放回您的增压容器.

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

原文链接:https://www.f2er.com/c/112881.html

猜你在找的C&C++相关文章