2013-08-21 62 views
2

我正在使用std :: array來爲最短路徑函數定義2D點。boost :: python轉換std :: array

typedef std::array<double, 2> point_xy_t; 
typedef std::vector<point_xy_t> path_t; 
path_t search(const point_xy_t& start, const point_xy_t& goal); 

現在,我最好的解決辦法是點轉換(STD ::數組)到std ::向量,並使用boost ::蟒蛇:: vector_indexing_suite爲:

bpy::class_<std::vector<double> >("Point") 
    .def(bpy::vector_indexing_suite<std::vector<double> >()) 
; 
bpy::class_<std::vector<std::vector<double>> >("Path") 
    .def(bpy::vector_indexing_suite<std::vector<std::vector<double>> >()) 
; 

它會可以索引或直接從/到std ::數組轉換爲/從python?

+1

[這](http://stackoverflow.com/a/15940413/1053968)的答案可能會有所幫助。它顯示瞭如何爲集合註冊自定義轉換器,包括多個維度。 –

回答

2

爲了給它pythonic外觀,我會使用boost::python::extract,tuplelist的組合。這裏是一個草圖:

static bpy::list py_search(bpy::tuple start, bpy::tuple goal) { 
    // optionally check that start and goal have the required 
    // size of 2 using bpy::len() 

    // convert arguments and call the C++ search method 
    std::array<double,2> _start = {bpy::extract<double>(start[0]), bpy::extract<double>(start[1])}; 
    std::array<double,2> _goal = {bpy::extract<double>(goal[0]), bpy::extract<double>(goal[1])}; 
    std::vector<std::array<double,2>> cxx_retval = search(_start, _goal); 

    // converts the returned value into a list of 2-tuples 
    bpy::list retval; 
    for (auto &i : cxx_retval) retval.append(bpy::make_tuple(i[0], i[1])); 
    return retval; 
} 

然後綁定應該是這樣的:

bpy::def("search", &py_search); 
相關問題