本文介绍了在Boost.Geometry中定义维度点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我很难定义和填充d维中的点。确切地说,我利用Boost.Geometry处理用户的任何维度,是不可能的(这是我从文档和他们的邮件列表看到的)。所以,我试图定义一个100D或10000D维度点。 这里的代码,我的尝试和他们的列表中的帮助开发: #include< boost / geometry.hpp> #include< boost / geometry / geometries / point.hpp> #include< boost / geometry / index / rtree.hpp> 命名空间bg = boost :: geometry; namespace bgi = boost :: geometry :: index; template< int CompileTimeDimension> void do_something() { typedef bg :: model :: point< float,CompileTimeDimension,bg :: cs :: cartesian&点; 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; } 错误是编译器看不到point的typedef。另一方面,我不能让typedef的维度是一个运行时变量(Boost不会让我(!))。我能做什么?除了使用另一个库,因为这是我在高维几何中见过的最糟糕的接口。 :/ 编译为: c ++ -I ../ bla.cpp -std = c ++ 0x -ftemplate-depth-170001 -o bla 解决方案 在我谦卑的观点中,Boost几何不适合高度的任意维度点> 4)。 编译器正确地抱怨不知道点,因为你没有在任何地方定义它。 解决方案:使用 模板别名 定义指向 p> template< std :: size_t D = 100> using point = bg :: model :: point< double,D,bg :: cs :: cartesian> 示例代码: #include< iostream> #include< boost / geometry.hpp> 命名空间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 return 0; } 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 解决方案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;} 这篇关于在Boost.Geometry中定义维度点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 12:27