'How can we get all the points stored in boost polygon

I am trying to iterate over all the points in the boost polygon. Is there an API to handle this.?



Solution 1:[1]

Here is a simple example of setting and retrieving the BOOST polygon vertex coordinates:

#include <boost/geometry.hpp>
namespace bg = boost::geometry;
typedef bg::model::d2::point_xy<double> boost_point;
typedef bg::model::polygon<boost_point> boost_polygon;

[...]

//setting vertices
boost_polygon poly;
bg::append(poly.outer(), boost_point(-1, -1));
bg::append(poly.outer(), boost_point(-1,  1));
bg::append(poly.outer(), boost_point( 1,  1));
bg::append(poly.outer(), boost_point( 1, -1));
bg::append(poly.outer(), boost_point(-1, -1));

//getting the vertices back
for(auto it = boost::begin(boost::geometry::exterior_ring(poly)); it != boost::end(boost::geometry::exterior_ring(poly)); ++it)
{
    double x = bg::get<0>(*it);
    double y = bg::get<1>(*it);
    //use the coordinates...
}

Solution 2:[2]

http://www.boost.org/doc/libs/1_62_0/libs/polygon/doc/gtl_polygon_concept.htm

template <typename T> point_iterator_type begin_points(const T& polygon)

Expects a model of polygon. Returns the begin iterator over the range of points that correspond to vertices of the polygon.

template <typename T> point_iterator_type end_points(const T& polygon)

Expects a model of polygon. Returns the end iterator over the range of points that correspond to vertices of the polygon.

Solution 3:[3]

You can also shorten the last loop from Olek's answer with

for( const auto& point : poly.outer() )
{
  double x = point.x();
  double y = point.y();
  //use the coordinates...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olek
Solution 2 Richard Hodges
Solution 3 Florian Richoux