Convert polygon boost :: geometry to an STL object

How to get boost :: geometry polygon in STL object?

I am sure this should be simple, because I cannot find examples in the documentation. However, I spent about 4 business days trying to do this tiny thing. I am new to C ++ (a long time R programmer), but these small data conversions confuse me.

Yes, there is a question whose title is very similar to mine: Getting point coordinates from a Boost Geometry polygon

But the code is so complex (and the poster constantly changed it so many times) that I cannot make heads or tails out of it, and I cannot imagine what other C ++ newbies will be able to do.

This is a simple example that should translate to some of the other boost :: geometry data types, so hopefully someone can follow it.

#include <iostream> #include <boost/geometry.hpp> #include <boost/geometry/geometries/polygon.hpp> #include <boost/geometry/geometries/adapted/boost_tuple.hpp> BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian) //One thing I tried is a function to use with `for_each_point()` so I set that up first. template <typename Point> void get_coordinates(Point const& p) { using boost::geometry::get; std::cout << get<0>(p) get<1>(p) << std::endl; } int main() { typedef boost::tuple<double, double> point; typedef boost::geometry::model::polygon<point> polygon; polygon poly; boost::geometry::read_wkt("polygon((2.0 1.3, 2.4 1.7, 2.8 1.8, 3.4 1.2, 3.7 1.6, 3.4 2.0, 4.1 3.0, 5.3 2.6, 5.4 1.2, 4.9 0.8, 2.9 0.7, 2.0 1.3))", poly); polygon hull; boost::geometry::convex_hull(poly, hull); // Now I know I can `dsv()` and print to the screen like this: using boost::geometry::dsv; std::cout << "hull: " << dsv(hull) << std::endl; // And/Or I can use my function with for_each_point() boost::geometry::for_each_point(hull, get_coordinates<point>); return 0; } 

How to get these coordinates in an STL container? I would prefer two std :: vector one for x and one for y, but something will do.

+7
source share
3 answers

Polygons are already in the STL container format, boost :: geometry :: polygon has an outer ring and inner rings stored by default std :: vector.

What can you wish for (considering your comments):

  polygon hull; boost::geometry::convex_hull(poly, hull); boost::geometry::for_each_point(boost::geometry::exterior_ring(hull), get_coordinates<point>); 

This will work if you fix the get_coordinates function (note the <<use):

  template <typename Point> void get_coordinates(Point const& p) { using boost::geometry::get; std::cout << get<0>(p) << ", " << get<1>(p) << std::endl; } 

And change your comment indicators to //; -)

+5
source

For example:

 boost::geometry::model::polygon<Point> polygon; polygon.outer().push_back( point ); 

So polygon.outer () is std :: vector (external as Barend Gehrels said). See here: link to enlarge

0
source

You can use iterators with boost models to iterate over all points and add them to any type of container you like.

Polygon models are a bit more complicated because they have one outer and potentially multiple inner rings.

Assuming you want all the points in the outer ring of the body object (or any polygon, including the original poly), just iterate over it and add point-to-point standard vectors.

If you need vector x and vector y, use the same method.

 ::std::vector< point > hullPoints; ::std::vector< double > hullXPoints; ::std::vector< double > hullYPoints; // the auto keyword makes declaring iterators easy for ( auto hullIterator = hull.outer().begin; hullIterator != hull.outer().end(); ++hullIterator ) { // for a vector of point types, just add the points one by one hullPoints.push_back( *hullIterator ); // for vectors of x and y, extract the x/y from the point hullXPoints.push_back( get<0>( *hullIterator ) ); hullYPoints.push_back( get<1>( *hullIterator ) ); } 
0
source

All Articles