问题
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
回答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;
}
来源:https://stackoverflow.com/questions/23118764/defining-a-dimensional-point-in-boost-geometry