c ++ Boost循环通过model::point的维度

c ++ Boost Loop through dimensions of model::point

我想知道是否有一种方法可以遍历提升点模型的维度。我正在尝试创建一个函数来对两个自定义点进行计算,具有可定义的维数。换句话说,每个点的维数将匹配,但它们不会是一个常量值。我想在每个维度上做相同的操作,所以我需要做一个循环来实现这个。

我想做的事情的一个例子是:

for(std::size_t dim = 0; dim < D; dim++){
    CoordinateType d = get<dim>();

    //do stuff to d

    set<dim>(d);

}

我知道这行不通,因为 d 不是编译时常量。

谢谢!

我对 boost geometry 不是很熟悉,但似乎你必须在编译时遍历坐标。做到这一点的方法是创建一个递归。下面的 class apply 将执行这样的递归并在点的每个坐标上调用一个函子。

你所要做的就是编写你自己的仿函数并重载它的 operator() 以防你想要专门针对特定坐标的行为。下面示例中的仿函数简单地打印坐标,并在读取第 3 个坐标时专门化。

#include <boost/geometry.hpp>
#include <iostream>
#include <type_traits>


namespace bg = boost::geometry;

template <int I>
using int_ = std::integral_constant<int, I>;

//recursive call that iterates the point and calls F on its coordinate
template <class Point, class F, std::size_t I = 0>
struct apply {

    static void call(Point& point, F& f) {
        f(point, int_<I>());
        apply<Point, F, I+1>::call(point, f);
    }
};

//specialisation to end the recursion
template <class CT, std::size_t DIM, class S, template <class, std::size_t, class> class Point, class F>
struct apply<Point<CT, DIM, S>, F, DIM> {

    static void call(Point<CT, DIM, S>& point, F& f){}
};

//interface for calling the function
template <class Point, class F>
void apply_functor(Point& point, F& f) {
    apply<Point, F>::call(point, f);
}

//example functor
template <class Point>
struct functor {

    template <class Index>
    void operator()(Point& point, Index I) {
        std::cout << "I am coordinate " << Index::value << " and my value is " << bg::get<Index::value>(point) << std::endl;
    }

//    used for overloading when reading the 3rd coordinate
    void operator()(Point& point, int_<2>) {
        std::cout << "I am coordinate " << 2 << " and I am specialised with value " << bg::get<2>(point) << std::endl;
    }
};


//3-dimensional point type
using point_type = bg::model::point<double, 3, bg::cs::cartesian>;

int main(int argc, char** argv) {
    point_type point(1,2,3);
    functor<point_type> f;
    apply_functor(point, f);

    return 0;
}

作为替代方法,我认为您应该能够将 Boost Geometry 点模型调整为 Fusion 序列。

Live On Coliru

#include <iostream>

namespace bg  = boost::geometry;
namespace fus = boost::fusion;

int main() {
    bg::model::point<double, 7, bg::cs::cartesian> p1;
    // set some 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 << ' '; });
}

版画

7 14 21 28 35 42 49 

这是非常通用的(并且会为您提供比 for_each 更多的算法)。在示例中,我并没有一路走下去,所以你实际上可以说 for_each(p1, f) 而不是 for_each(as_vector(p1), f) 但你知道...... reader.

的谚语练习

这里涉及一些扩展 "glue" 代码。我只是按照文档 here

在此处查看完整清单:

Live On Coliru

#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_);
                        }
                };
            };
    }

} }