3

The boost::geometry::model::point takes as a compile-time argument the dimension of the point. For instance,

typedef bg::model::point<float, 2, bg::cs::cartesian> point;

Is there any way of specifying the dimension at run time, say, depending on input given to program?

My goal is to use the rtree data structure in boost::geometry::index with arbitrary dimensions. Is it possible to write a custom point class with this feature, or would the type system prevent me from doing this?

genpfault
  • 51,148
  • 11
  • 85
  • 139
cankosa
  • 43
  • 3

2 Answers2

1

There is no way, facilitated by the library.

You can always employ your own type erasure. This will take some effort, and depending on how it's executed, possibly some performance.

That's actually also the reason this doesn't "jell" with the library design. The library focuses strongly on performance through genericity.

Contrary to what you expect this does not support runtime polymorphism, because that would hamper performance. Instead, strictly compiletime polymorphism is used. The compiler can inline and "see through" all the code paths to generate optimal code.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    Thanks. As a side note, the following R-tree library allows arbitrary dimensions (at runtime): https://libspatialindex.github.io/ No idea about its performance though – cankosa Dec 02 '15 at 22:56
  • I wanted to use boost, but I don't know the dimensionality at compile time and I can't figure out how to do this with boost. The dimensionality depends on the data set chosen by the user... (also, the rstar index in boost appears to be low-quality) – Has QUIT--Anony-Mousse Feb 10 '16 at 22:56
  • Actually it could improve performance. This strongly depends on a case but if e.g. one would like to store points with dimension defined at runtime the point would have to allocate memory and store coordinates in some other place in memory. In such case converting dynamic-size points to static-size points (storing coordinates locally) before insertion into the rtree would allow the rtree to store the coordinate data of all points in a fragment of space (rtree node) in one place in memory and therefore improve the locality of the data and decrease the number of cache misses during query. – Adam Wulkiewicz Dec 10 '17 at 19:43
  • In the most simple case a solution would be to have an if condition or a factory generating a class doing the job and e.g. wrapping all dimension-dependent code behind some run-time interface. The R-tree is not useful for big dimensions anyway so it's not that the dimension will be arbitrary. – Adam Wulkiewicz Dec 10 '17 at 19:49
0

I'm not sure why the answer "not possible" is accepted to this question. The question simply asks whether this is possible or not, rather than it is performant or mediocre. I'm currently using rtree data structure of boost::geometry supporting [1-6] dimensions as an internal container in my classes.

template <typename T, std::size_t N>
using BoostHyperPoint = bg::model::point<T, N, bg::cs::cartesian>;

You can almost trivially define such a templated point as in the above. With a little more effort, you can have a generic rtree class.