Boost logo

Geometry :

Subject: [geometry] [point_iterator] make point iterator bidirectional
From: Menelaos Karavelas (menelaos.karavelas_at_[hidden])
Date: 2014-05-05 03:21:00


Hello all.

Before getting into the details, let me give some background. During the
past couple of months I have implemented an iterator for the points of a
geometry. The code is already in the develop branch, but it is not yet
documented (which means "use at your own risk", at least for now...).
The idea is to have an iterator to iterate over all points of a
geometry, and the supported geometries are: linestrings, rings,
polygons, multi-points, multi-linestrings and multi-polygons. To use the
iterator one can simply write:
     bg::point_iterator<Geometry> first = bg::points_begin(g); //
non-const first
     bg::point_iterator<Geometry> last = bg::points_end(g); // non-const
past-the-end

     bg::point_iterator<Geometry const> const_first =
bg::points_begin(g); // const first
     bg::point_iterator<Geometry const> const_last = bg::points_end(g);
// const past-the-end
The points' iterator was implemented as a forward iterator.
The corresponding implementation file is:
*
https://github.com/boostorg/geometry/blob/develop/include/boost/geometry/iterators/point_iterator.hpp
but there is also:
*
https://github.com/boostorg/geometry/blob/develop/include/boost/geometry/iterators/point_iterator_type.hpp
and other source code files involved....
If people are interested I can describe the implementation design in detail.

This email is about follow-up work in this direction. During the past
couple of days I changed/modified/updated the implementation to make the
point iterator bidirectional. So now one can use operator-- on point
iterators, can use it in algorithms that require a bidirectional
iterator (such std::reverse), etc. In addition, I have implemented two
new free functions (the supported geometries are as above):
** points_front: returns the first point of a (non-empty) geometry
** points_back: returns the last point of a (non-empty) geometry
So in order to use them, you may simply write:
     bg::point_type<Geometry>::type p = bg::points_front(g);
     bg::point_type<Geometry>::type q = bg::points_back(g);
(I have omitted the "typename" keyword up-front).

Finally, I have implemented a reverse iterator for points, and the
corresponding free functions: points_rbegin and points_rend. To use them
simply do:
     bg::point_reverse_iterator<Geometry> rfirst = bg::points_rbegin(g);
// non-const reverse first
     bg::point_reverse_iterator<Geometry> rlast = bg::points_rend(g); //
non-const reverse past-the-end

     bg::point_reverse_iterator<Geometry const> const_rfirst =
bg::points_rbegin(g); // const reverse first
     bg::point_reverse_iterator<Geometry const> const_rlast =
bg::points_rend(g); // const reverse past-the-end

I have created a pull request for this:
https://github.com/boostorg/geometry/pull/21

In there you will find:
** changes in the unit tests (re-arranged/re-factored code; added tests
for points_front/back and point_reverse_iterator; added code to test the
iterators as bidirectional). The corresponding files are in test/iterators:
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/test/iterators/concatenate_iterator.cpp
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/test/iterators/flatten_iterator.cpp
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/test/iterators/point_iterator.cpp
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/test/iterators/test_iterator_common.hpp
(contains common factored-out code)

** changes in the implementation:
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/include/boost/geometry/iterators/concatenate_iterator.hpp
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/include/boost/geometry/iterators/flatten_iterator.hpp
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/include/boost/geometry/iterators/point_iterator.hpp
(contains points_front/points_back)
*
https://github.com/mkaravel/geometry/blob/feature/point_iterator/include/boost/geometry/iterators/point_reverse_iterator.hpp
(contains implementation of reverse iterator)

I look forward to comments/suggestions. I would suggest to put your
comments/suggestions on GitHub, using GitHub's comments' feature
available through the pull request.

All the best,

- m.


Geometry list run by mateusz at loskot.net