 # Geometry :

Subject: Re: [geometry] generic for_each and num proposal
From: Mats Taraldsvik (mats.taraldsvik_at_[hidden])
Date: 2013-07-28 06:23:33

Hi,

On 07/26/2013 11:54 PM, Michael Winkelmann wrote:
> 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 */
> });

It would be very useful to expand on the for_each algorithms. However,
as I understand the boost.geometry documentation and your proposal,
there is a subtle but important difference: the current for_each_point
algorithm work on a point _concept_, while your for_each<point>
algorithm work on a concrete point type (in this case
bg::model::d2::point_xy<double>.

What if I used e.g. std::pair to represent points? I'd have to remember
the exact type used for points in the polygon/ring/linestring/segment...
I might have two polygons that represents points in differents ways, and
then I'd have to implement the algorithm twice
(for_each<bgm::d2::point_xy<double>> and for_each<std::pair<double,double>>.

I'm not sure if this is solvable in the generic case until we get proper
Concepts (lite), which, combined with generic lambdas would make a
concepts-based interface.

>
> 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
>