Boost::Geometry union simplification - how it works?

前端 未结 1 1582
陌清茗
陌清茗 2021-02-08 15:36

There is great library for geometry in Boost. It allows also to draw SVG images. I want to use it in some project of mine but it works really strange for me (see image below).

相关标签:
1条回答
  • 2021-02-08 16:12

    I think there are several problems with the code:

    • The polygons you are defining are:

    1 1
    1 0

    That is:

    three two
    one    -
    

    So the expected result is different from pic2.

    • Polygons should be closed, and directed clockwise.

    You are missing the closing point and the third polygon is not directed clockwise. Take a look at the correct method. On this example, you should call it for every polygon you define.

    • You cannot use the same argument for input and output when using _union.

    You should use a temporary variable:

      boost::geometry::union_(one, two, outputTmp);    
      boost::geometry::union_( outputTmp, three, output);  
    
    • Your expected result may not be the algorithm result.

    After executing the corrected code, the result is:

    simplify result

    This may be a valid simplifcation of your polygon. See the Ramer–Douglas–Peucker algorithm.

    After performing those modifications, below is the resulting main()

    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 ); 
    }
    
    0 讨论(0)
提交回复
热议问题