1

I am struggling to define and fill a point in d dimensions. To be exact, I exploited that letting Boost.Geometry handle any dimension by the user, is impossible (that is what I saw from the docs and their mailing list). So, I am trying to define a 100D or 10000D dimension point.

Here is the code, where my attempts and the help from their list developed:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
#include <boost/geometry/index/rtree.hpp>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;

template <int CompileTimeDimension>
void do_something()
{
    typedef bg::model::point<float, CompileTimeDimension, bg::cs::cartesian> point;
    bgi::rtree<point, bgi::linear<8> > rt;
}

template <std::size_t D, std::size_t N>
struct fill
{
    template <typename Point>
    static void apply(Point& p, typename bg::coordinate_type<Point>::type const& v)
    {
        bg::set<D>(p, v);
        fill<D + 1, N>::apply(p, v);
    }
};
template <std::size_t N>
struct fill<N, N>
{
    template <typename Point>
    static void apply(Point&, typename bg::coordinate_type<Point>::type const&) {}
};


int main()
{
    int M;
    M = 100;
    if ( M == 100 )
        do_something<100>();
        else if ( M == 10000 )
        do_something<10000>();
        else
        std::cerr << "invalid dimension!";
    point p;    
    if ( M == 100 )
        fill<0, 100>::apply(p, 5);
        else if ( M == 10000 )
        fill<0, 10000>::apply(p, 5);
        else
        std::cerr << "invalid dimension!";  
    return 0;
}

The error is that the compiler can not see the typedef of "point". On the other hand, I can not make dimension of the typedef to be a run-time variable (Boost won't let me(!)). What can I do? Except from using another library, since this is the worst interface I have ever seen in higher dimensions geometry. :/

Compiled as: c++ -I ../ bla.cpp -std=c++0x -ftemplate-depth-170001 -o bla

gsamaras
  • 71,951
  • 46
  • 188
  • 305

2 Answers2

2

I found the docs a bit obtuse. There are some macros (ick!) that help enable the Point concept for low-dimensioned types. This is the example I wish Boost provided (C++17):

namespace boost::geometry::traits {
template<typename T, int D> struct tag<Eigen::Matrix<T, D, 1>> { using type = point_tag; };
template<typename T, int D> struct dimension<Eigen::Matrix<T, D, 1>> : boost::mpl::int_<D> {};
template<typename T, int D> struct coordinate_type<Eigen::Matrix<T, D, 1>> { using type = T; };
template<typename T, int D> struct coordinate_system<Eigen::Matrix<T, D, 1>> { using type = boost::geometry::cs::cartesian; };

template<typename T, int D, int Dim>
struct access<Eigen::Matrix<T, D, 1>, Dim> {
    static_assert(0 <= Dim && Dim < D, "Out of range");
    using Point = Eigen::Matrix<T, D, 1>;
    using CoordinateType = typename coordinate_type<Point>::type;
    static inline const CoordinateType& get(Point const& p) { return p[Dim]; }
    static inline void set(Point& p, CoordinateType const& value) { p[Dim] = value; }
};
} // namespace boost::geometry::traits
Ben
  • 9,184
  • 1
  • 43
  • 56
1
  • In my humble opionion, Boost geometry is not proper for high arbitrary dimension points (i.e., dimension > 4). Nevertheless, points with high arbitrary dimension are supported due to its generic nature.
  • The compiler is rightfully complaining about not knowing point since you haven't define it anywhere.

Solution: Use template aliases to define point as an arbitrary dimension boost point:

template <std::size_t D = 100>
using point = bg::model::point<double, D, bg::cs::cartesian>;

example code:

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

namespace bg = boost::geometry;

template <std::size_t D = 100>
using point = bg::model::point<double, D, bg::cs::cartesian>;

int main()
{
    int const M = 2;
    point<M> p;
    p.set<0>(1.0);
    p.set<1>(2.0);
    double x = p.get<0>();
    double y = p.get<1>();
    std::cout << x << ", " << y << std::endl;
    return 0;
}
101010
  • 41,839
  • 11
  • 94
  • 168
  • I agree with that opinion. However, I have to fill the point. I will have points in 10000 dimensions. This means, that I have to use the fill(), I have above. When having your template..using point.., the fill() and the main, I receive this error: expected unqualified-id before ‘using’ – gsamaras Apr 17 '14 at 10:21
  • Damn, just noticed, that fill() will fill the point with the same value. Too much effort to use Boost for my purpose and I suspect their algorithm doesn't scale, so I will use another library. – gsamaras Apr 17 '14 at 10:46