2017-03-04 64 views
0

我想爲C++ Computational Topology庫GUDHI(http://gudhi.gforge.inria.fr)設計一個Cython包裝。 GUDHI類爲其參數提供了其他類,這些類將其他子類傳遞給它們的方法。所以相當凌亂,直接推到Cython。下面有一個代碼示例。爲複雜層次結構的C++類編寫一個C++簡化包裝,以便Cython可以調用它

我向cython google group發佈了一個問題,有人建議我用C++編寫一個簡化的包裝來隱藏Cython的這個複雜性。下面的評論的text如下。

如果事情變得複雜,另一種方法是在C++中編寫簡化的包裝並調用它們。 (在Cython支持任何C++之前,這是必須的,如果使用了深奧的C++特性,即使它只是一個簡單的聲明幾個typedef,它仍然可能是有用的。在下面的代碼中,它定義了(以及未定義和重新定義(!))宏以應對那些否則會是非常冗長的代碼,這可能是你最好的選擇。)你也可以執行casts [1](動態或其他)來轉換Cython不知道相關的類型。

這裏是一個代碼示例,讓您瞭解所涉及的層次結構。

typedef CGAL::Epick_d< CGAL::Dimension_tag<2> > Kernel; 

--- needs to be passed into 


Gudhi::alpha_complex::Alpha_complex<Kernel> alpha_complex_from_points(points, alpha_square_max_value); 

Epick_d.h 

--- Epick_d.h depends upon these libraries 

#include <CGAL/NewKernel_d/Cartesian_base.h> 
#include <CGAL/NewKernel_d/Cartesian_static_filters.h> 
#include <CGAL/NewKernel_d/Cartesian_filter_K.h> 
#include <CGAL/NewKernel_d/Wrapper/Cartesian_wrap.h> 
#include <CGAL/NewKernel_d/Kernel_d_interface.h> 
#include <CGAL/internal/Exact_type_selector.h> 
#include <CGAL/Interval_nt.h> 

--- Sample source code for Epick_d.h 

namespace CGAL { 
#define CGAL_BASE \ 
Cartesian_filter_K< Cartesian_base_d<double, Dim>, \ 
Cartesian_base_d<Interval_nt_advanced, Dim>, \ 
Cartesian_base_d<internal::Exact_field_selector<double>::Type, Dim> \ 
> 
template<class Dim> 
struct Epick_d_help1 
: CGAL_BASE 
{ 
CGAL_CONSTEXPR Epick_d_help1(){} 
CGAL_CONSTEXPR Epick_d_help1(int d):CGAL_BASE(d){} 
}; 
#undef CGAL_BASE 
#define CGAL_BASE \ 
Cartesian_static_filters<Dim,Epick_d_help1<Dim>,Epick_d_help2<Dim> > 
template<class Dim> 
struct Epick_d_help2 
: CGAL_BASE 
{ 
CGAL_CONSTEXPR Epick_d_help2(){} 
CGAL_CONSTEXPR Epick_d_help2(int d):CGAL_BASE(d){} 
}; 
#undef CGAL_BASE 
#define CGAL_BASE \ 
Kernel_d_interface< \ 
Cartesian_wrap< \ 
Epick_d_help2<Dim>, \ 
Epick_d<Dim> > > 
template<class Dim> 
struct Epick_d 
: CGAL_BASE 
{ 
CGAL_CONSTEXPR Epick_d(){} 
CGAL_CONSTEXPR Epick_d(int d):CGAL_BASE(d){} 
}; 
#undef CGAL_BASE 
} 
#endif 

我不知道如何設計一個C++包裝,將與用Cython兼容,也將隱藏用Cython的層次結構。

這是我的想法,但我不確定這是什麼意思的簡化包裝。所以GUDHI類需要一系列n維點,然後對它們進行一些幾何計算。所以現在如果我想直接包裝一個GUDHI類,例如Simplex_tree(subclass),那麼我需要通知Cython這個。相反,我可以編寫一個C++類,只需要獲取點數組,計算結果,然後返回一個數組。因此,在一些僞代碼類似

Class(*array) 

    constructor(*array) 

     loop: 

      take values from *array and create CGAL::Point_d 

      push new CGAL::Point_d to std::vector<CGAL::Point_d> 

     include all of the hierarchical class operations here. 

    method_1(std::vector) 

     include all of the hierarchical operations here, but 
     return the result as a simple array or struct. 

會我再能來包裝這個類用Cython,而不必通過所有的類層次結構的,因爲我只是傳遞一個數組指針函數?

回答

0

這就是最後GUDHI version 2.0.0所做的。 包裝類Alpha_complex_interface(不再模板化)包裝Alpha_complex>以在每個維度中工作。 Alpha_complex_interface類然後被cython化(參見alpha_complex.pyx) 然後點通過std :: vector> &進行交換。 如果您對我的做法有任何疑問,或者您需要其他界面,請不要猶豫。

相關問題