在CFFI

时间:2016-10-08 22:14:21

标签: python c cffi

我尝试使用CFFI在现有的C code周围构建一个python接口。像往常一样,为了性能而修剪C代码,它充满了广泛的宏和typedef。

ATM我正在复制以下struct

#define DIM     3
typedef double  REAL;
struct Object_structure {
  int numpoints;
  REAL (* vertices)[DIM];
  int * rings;
};
typedef struct Object_structure * Object;

我试图调用的函数需要Object类型的参数。

REAL gjk_distance(
   Object obj1, REAL (* tr1)[DIM+1],
   Object obj2, REAL (* tr2)[DIM+1],
   REAL wpt1[DIM], REAL wpt2[DIM],
   struct simplex_point * simplex, int use_seed
   );

我编写了以下python类来表示这样的对象/结构,但是我无法将其转换为预期的cdata对象。 (现在我只考虑一个UnitCube,但最终我想要概括一下。)

class Box:
    def __init__(self, pos):
        self._weakkeydict = weakref.WeakKeyDictionary()
        self.numpoints = 8
        self.rings = [
            8, 12, 16, 20, 24, 28, 32, 36,
            3, 1, 4, -1,
            0, 2, 5, -1,
            1, 3, 6, -1,
            2, 0, 7, -1,
            7, 5, 0, -1,
            4, 6, 1, -1,
            5, 7, 2, -1,
            6, 4, 3, -1]
        x, y, z = pos
        self.vertices = [
            [x+0, y+0, z+0],
            [x+1, y+0, z+0],
            [x+1, y+1, z+0],
            [x+0, y+1, z+0],
            [x+0, y+0, z+1],
            [x+1, y+0, z+1],
            [x+1, y+1, z+1],
            [x+0, y+1, z+1],
        ]

    @property
    def cdata(self):
        self._weakkeydict.clear()

        #ptr_numpoints = ffi.new("int", self.numpoints)
        ptr_rings = ffi.new("int[]", self.rings)
        vertices = [ffi.new("REAL[3]", v) for v in self.vertices]
        ptr_vertices = ffi.new("REAL *[3]", vertices )

        ptr_obj = ffi.new("Object", { 
            'numpoints': self.numpoints,
            'rings': ptr_rings,
            'vertices': ptr_vertices})
        self._weakkeydict[ptr_obj] = (ptr_rings, ptr_vertices, vertices)
        return ptr_obj

通过以上操作,我在调用IndexError: too many initializers for 'double *[3]' (got 8)时获得了ptr_vertices = ffi.new("REAL *[3]", vertices )

box1 = Box((0,0,0)) 
box2 = Box((10,0,0))
d = lib.gjk_distance( 
        [box1.cdata], ffi.NULL,
        [box2.cdata], ffi.NULL,
        ffi.NULL, ffi.NULL, 
        ffi.NULL,  0    )

对我而言,似乎尺寸以某种方式切换了。因为它应该是一个包含3个元素项的8元素数组。 我希望有人能指出我正确的方向。

2 个答案:

答案 0 :(得分:1)

如果您要创建单个项目,请使用ffi.new('REAL(*)[3]', [1, 2, 3])*周围的括号很重要。

在C中,类型REAL(*)[3]表示指向REAL的数组(size = 3)的指针,而REAL*[3]表示指向real的指针的数组(size = 3)。有关详细信息,请参阅C pointer to array/array of pointers disambiguation

现在,您正在创建一系列项目,而不是expects an array typeas you have already discovered。这可以比较为:

ffi.new('int*', 1)    # ok
ffi.new('int[]', 1)   # wrong
ffi.new('int*', [1, 2, 3])    # wrong
ffi.new('int[]', [1, 2, 3])   # ok

ffi.new('REAL(*)[3]', [0.1, 0.2, 0.3])    # ok
ffi.new('REAL[][3]', [0.1, 0.2, 0.3])     # wrong
ffi.new('REAL(*)[3]', [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]])    # wrong
ffi.new('REAL[][3]', [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]])     # ok

答案 1 :(得分:0)

显然ptr_vertices = ffi.new("REAL[][3]", self.vertices )是要走的路。现在似乎有效。