 # Geometry :

Subject: [geometry] generic for_each and num proposal
From: Michael Winkelmann (michael-winkelmann_at_[hidden])
Date: 2013-07-26 17:54:22

Hi all,

I've played around a lot with boost::geometry and find the
for_each_point and for_each_segment algorithm very useful.
However, I think of a more generic approach which will makes most sense
together with C++ lambdas:

Let's assume we have a for_each template with the following type
definitions:

template<class SubGeometry, class Geometry, class Functor>
for_each(Geometry& g, Functor f) { ... }

typedef boost::geometry::model::d2::point_xy<double> point;
typedef boost::geometry::model::polygon<point> polygon;
typedef boost::geometry::model::ring<point> ring;
typedef boost::geometry::model::segment<point> segment;

E.g., if you have a polygon and want to iterate over each ring:
polygon myPolygon(...);
for_each<ring>(myPolygon,[&](const ring& r)
{
/* code here */
});

Or, if you want to iterate over each segment in a ring:
ring myRing(...);
for_each<segment>(myRing[&](const segment& s)
{
/* code here */
});

For the num_points algorithm, there might be an analogous generic
definition:

template<class SubGeometry, class Geometry>
size_t num(const Geometry& _g) { ... }

size_t a = num<ring>(myPolygon); // Returns number of rings in polygon
size_t b = num<segment>(myRing); // Returns number of segments in ring
size_t c = num<polygon>(myPolygon); // Should return 1