我正在通过Cython将Python与一个名为子弹物理的C++库进行接口。我有相当多的工作,但有一个问题是困扰我。这里是一个在多个上下文中发生的问题的例子。
一个子物理.h文件声明了一个方法,我将其复制并合并到我的cdefs中,如下所示:
cdef cppclass btSliderConstraint:
btSliderConstraint *btSliderConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB, bool useLinearReferenceFrameA)问题是如何在对此方法的Cython调用中指定rbA和rbB引用(& rbA和& rbB)。我有很多指向btRigidBody对象的指针,但是这个方法的声明要求参数是引用(除非我弄错了)。
如果我只想为btRigidBody和rbB提供指向rbB对象的指针,那么cython编译器就会抱怨(这是可以理解的):
Error compiling Cython file:
------------------------------------------------------------
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(b1, b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:41: Cannot assign type 'btRigidBody *' to 'btRigidBody'我试图施放或取消尊重等人都没有做过这项工作。在普通的C++中,这似乎很容易做到,但在Cython中,我尝试过的任何东西似乎都不起作用。
如果我能够简单地声明一个类型为btRigidBody的变量,如下所示:
btRigidBody rbA
然后,我相信我可以把它作为一个论点来传递,编译器不会抱怨。我这样做是在其他情况下,它是有效的。但是,我不想在这里这样做,因为我已经有了指向要作为参数传递的对象的指针,而且,这样做还需要为btRigidBody对象存在一个“空构造函数”,并且库不提供不带参数的构造函数,而且出于可维护性的原因,我不想对库进行修改。
那么,如何在Cython中将指针转换为我所拥有的btRigidBody对象,转换为我需要的btRigidBody引用?
编辑:
使用*来取消引用指针在Cython中是行不通的(虽然我认为它在C++中会起作用)。在Cython中,会产生一系列令人困惑的错误:
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:34: Non-trivial keyword arguments and starred arguments not allowed in cdef functions.
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:38: Cannot convert 'btRigidBody *' to Python object
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:43: Cannot convert 'btRigidBody *' to Python object
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:48: Cannot convert 'btTransform' to Python object
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:53: Cannot convert 'btTransform' to Python object
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:34: Cannot convert Python object to 'btSliderConstraint *'
Error compiling Cython file:
------------------------------------------------------------
...
cdef btRigidBody *b1
cdef btRigidBody *b2
# bs.bodies is an array of pointers to btRigidBody objects, nbi and ji1 are integer indices
b1 = bs.bodies[nbi]
b2 = bs.bodies[ji1]
motor = new btSliderConstraint(*b1, *b2, tra, trb, 1);
^
------------------------------------------------------------
fun4.pyx:922:34: Storing unsafe C derivative of temporary Python reference发布于 2017-04-04 23:57:30
由于Cython的目标是保持Python语法,所以不可能使用*作为指针取消引用。相反,使用[]。以下是所希望的:
运动=新btSliderConstraint(bs.bodiesnbi,bs.bodiesji1,tra,trb,1);
https://stackoverflow.com/questions/43219293
复制相似问题