c++ - c ++ Boost Loop through dimensions of model::point -
i wondering if there way loop through dimensions of boost point model. trying create function calculations on 2 custom points, definable number of dimensions. in other words number of dimensions of each point match, not constant value. want same operations on each dimension, need loop in order achieve this.
an example of want want be:
for(std::size_t dim = 0; dim < d; dim++){ coordinatetype d = get<dim>(); //do stuff d set<dim>(d); }
i know not work because d
not compile-time constant.
thanks!
as alternative approach thought should able adapt boost geometry point model fusion sequence.
#include <iostream> namespace bg = boost::geometry; namespace fus = boost::fusion; int main() { bg::model::point<double, 7, bg::cs::cartesian> p1; // set nice values p1.set<0>(7); p1.set<1>(14); p1.set<2>(21); p1.set<3>(28); p1.set<4>(35); p1.set<5>(42); p1.set<6>(49); fus::for_each(fus::as_vector(p1), [](double x) { std::cout << x << ' '; }); }
prints
7 14 21 28 35 42 49
this pretty versatile (and give many more algorithms for_each
). in sample i've not gone way can for_each(p1, f)
instead of for_each(as_vector(p1), f)
know... proverbial exercise reader.
there's bit of extension "glue" code involved here. followed documentation here
see full listing here:
#include <boost/geometry/core/cs.hpp> #include <boost/geometry/geometries/point.hpp> #include <boost/fusion/include/for_each.hpp> #include <boost/fusion/include/as_vector.hpp> namespace bg_to_fusion { using namespace boost; struct bg_point_tag; struct example_struct_iterator_tag; template<typename point, int pos> struct point_iterator : fusion::iterator_base<point_iterator<point, pos> > { boost_static_assert(pos >=0 && pos <geometry::traits::dimension<typename remove_cv<point>::type>::value); typedef point point_type; typedef mpl::int_<pos> index; //typedef fusion::random_access_traversal_tag category; typedef fusion::forward_traversal_tag category; point_iterator(point& p) : point_(p) {} point& point_; }; } namespace boost { namespace fusion { // tag dispatch namespace traits { template <typename t, size_t dims, typename cs> struct tag_of<geometry::model::point<t, dims, cs> > { typedef bg_to_fusion::bg_point_tag type; }; template <typename point, int pos> struct tag_of<bg_to_fusion::point_iterator<point, pos> > { typedef bg_to_fusion::example_struct_iterator_tag type; }; } namespace extension { ////////////////////////////////////////////////////// // point extension implementations template<> struct is_sequence_impl<bg_to_fusion::bg_point_tag> { template<typename t> struct apply : mpl::true_ {}; }; template <> struct size_impl<bg_to_fusion::bg_point_tag> { template <typename point> struct apply : mpl::integral_c<size_t, geometry::traits::dimension<typename remove_cv<point>::type>::value> { }; }; // begin template<> struct begin_impl<bg_to_fusion::bg_point_tag> { template<typename point> struct apply { typedef typename bg_to_fusion::point_iterator<point, 0> type; static type call(point& p) { return type(p); } }; }; // end template<> struct end_impl<bg_to_fusion::bg_point_tag> { template<typename point> struct apply { typedef typename bg_to_fusion::point_iterator<point, geometry::traits::dimension<point>::value> type; static type call(point& p) { return type(p); } }; }; //////////////////////// // iterator extension implementations // value_of template <> struct value_of_impl<bg_to_fusion::example_struct_iterator_tag> { template<typename iterator> struct apply; template<typename point, int pos> struct apply<bg_to_fusion::point_iterator<point, pos> > { typedef typename geometry::traits::coordinate_type<typename remove_cv<point>::type>::type type; }; }; // deref template<> struct deref_impl<bg_to_fusion::example_struct_iterator_tag> { template<typename iterator> struct apply; template<typename point, int pos> struct apply<bg_to_fusion::point_iterator<point, pos> > { typedef typename geometry::traits::coordinate_type<typename remove_cv<point>::type>::type coordinate_type; //typedef typename mpl::if_<is_const<point>, coordinate_type const&, coordinate_type&>::type type; typedef coordinate_type type; static type call(bg_to_fusion::point_iterator<point, pos> const& it) { return it.point_.template get<pos>(); } }; }; // next template<> struct next_impl<bg_to_fusion::example_struct_iterator_tag> { template<typename iterator> struct apply { typedef typename iterator::point_type point_type; typedef typename iterator::index index; typedef typename bg_to_fusion::point_iterator<point_type, index::value + 1> type; static type call(iterator const& i) { return type(i.point_); } }; }; } } }
Comments
Post a Comment