C++ 几何体联合简化-它是如何工作的?

C++ 几何体联合简化-它是如何工作的?,c++,boost,boost-geometry,C++,Boost,Boost Geometry,Boost中有一个很棒的几何图形库。它还允许绘制SVG图像。我想在我的某个项目中使用它,但它对我来说真的很奇怪(见下图) 所以我们有3个像素点,在2d空间中表示为正方形多边形 1 1 0 1 pic1 我们想从他们那里得到一个并集,并简化它,这样当我们缩放它时,我们会得到一个三角形,就像 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 pic2 但我们得到的是: 其中黄色圆点线表

Boost中有一个很棒的几何图形库。它还允许绘制SVG图像。我想在我的某个项目中使用它,但它对我来说真的很奇怪(见下图)

所以我们有3个像素点,在2d空间中表示为正方形多边形

 1 1
 0 1
pic1

我们想从他们那里得到一个并集,并简化它,这样当我们缩放它时,我们会得到一个三角形,就像

1 1 1 1 1 1
1 1 1 1 1 1  
1 1 1 1 1 1
0 1 1 1 1 1 
0 0 1 1 1 1 
0 0 0 1 1 1
pic2

但我们得到的是:

其中黄色圆点线表示并集,绿色表示简化

源代码:

#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/algorithms/envelope.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");
}

int main()
{

    // create points (each point == square poligon)
    boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > one, two, three;

    boost::geometry::read_wkt(
        "POLYGON((1 1, 1 0, 0 0, 0 1))", one);

    boost::geometry::read_wkt(
        "POLYGON((2 2, 2 1, 1 1, 1 2))", two);

    boost::geometry::read_wkt(
        "POLYGON((1 1, 1 2, 0 2, 0 1))", three);

    // 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=))
    boost::geometry::union_(one, two, output);
    boost::geometry::union_( output , three, output);

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

    // create an svg image
    create_svg("make_envelope.svg", simpl, output );
}
这段代码创建了这样的图像:

对于3分,它返回的图像与答案相同:


我认为代码有几个问题:

  • 要定义的多边形包括:
11
10

即:

因此,预期结果与pic2不同

  • 多边形应该是闭合的,并且是顺时针方向的
缺少闭合点,第三个多边形不是顺时针方向。看看这个方法。在本例中,应该为定义的每个多边形调用它

  • 使用_union时,不能对输入和输出使用相同的参数
应使用临时变量:

  boost::geometry::union_(one, two, outputTmp);    
  boost::geometry::union_( outputTmp, three, output);  
  • 您的预期结果可能不是算法结果
执行纠正后的代码后,结果为:

这可能是多边形的有效简化。看

执行这些修改后,下面是生成的main()

intmain()
{
//创建点(每个点==正方形多边形)
几何:模型:多边形一,二,三;
boost::geometry::read_wkt(“多边形((11,10,0,01))”,1);
boost::geometry::read_wkt(“多边形((22,21,11,12))”,2);
boost::geometry::read_wkt(“多边形((11,12,02,01))”,三个);
boost::geometry::correct(一);
boost::geometry::correct(两个);
几何图形:正确(三);
//为连接点结构创建容器
boost::geometry::model::multi_polygonoutputTmp,output,siml;
//一个接一个的连接点(因为有一天我们会有很多=)
boost::geometry::union_(一,二,outputmp);
boost::geometry::union_u3;(outputTmp,three,output);
//简化连接结构
boost::geometry::simplify(输出,siml,0.5);
//创建svg图像
创建_svg(“make_envelope.svg”,siml,output);
}

用更多的代码更新了Q,现在可以正常工作了,但是你能为它提供一个修复程序,说明如何在poligons创建时删除内部的gark绿线吗?得到了想法=)发布了我的解决方案
three two
one    -
  boost::geometry::union_(one, two, outputTmp);    
  boost::geometry::union_( outputTmp, three, output);  
int main() 
{
  // create points (each point == square poligon)     
  boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > one, two, three;  
  boost::geometry::read_wkt(         "POLYGON((1 1, 1 0, 0 0, 0 1))", one);  
  boost::geometry::read_wkt(         "POLYGON((2 2, 2 1, 1 1, 1 2))", two); 
  boost::geometry::read_wkt(         "POLYGON((1 1, 1 2, 0 2, 0 1))", three);  
  boost::geometry::correct(one);
  boost::geometry::correct(two);
  boost::geometry::correct(three);

  // create a container for joined points structure  
  boost::geometry::model::multi_polygon< boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > > outputTmp, output, simpl;      
  // join points one by one (because one day we would have many=))    
  boost::geometry::union_(one, two, outputTmp);    
  boost::geometry::union_( outputTmp, three, output);    
  // simplify joined structure  
  boost::geometry::simplify(output, simpl, 0.5);   
  // create an svg image   
  create_svg("make_envelope.svg", simpl, output ); 
}