gpt4 book ai didi

C++ 方法需要引用,无法让 Cython 提供一个

转载 作者:搜寻专家 更新时间:2023-10-31 00:28:57 25 4
gpt4 key购买 nike

我正在通过 Cython 将 Python 连接到名为 Bullet Physics 的 C++ 库。我有相当多的工作但有一个问题让我烦恼。这是在多个上下文中发生的问题的示例。

Bullet Physics .h 文件声明了一个方法,我将其复制并合并到我的 cdef 中,如下所示:

cdef cppclass btSliderConstraint:
btSliderConstraint *btSliderConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB, bool useLinearReferenceFrameA)

问题是如何在对此方法的 Cython 调用中指定 rbA 和 rbB 引用(&rbA 和 &rbB)。我有很多指向 float 的 btRigidBody 对象的指针,但此方法的声明要求参数是引用(除非我弄错了)。

如果我尝试只为 rbA 和 rbB 提供指向 btRigidBody 对象的指针,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 类型的变量:

cdef 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

最佳答案

由于 Cython 的目标是保留 Python 语法,所以不可能使用 * 来取消引用指针。而是使用 []。以下是所需的:

motor = new btSliderConstraint(bs.bodies[nbi][0], bs.bodies[ji1][0], tra, trb, 1);

关于C++ 方法需要引用,无法让 Cython 提供一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com