【发布时间】:2021-03-21 10:38:48
【问题描述】:
我正在尝试用 Cython 包装 Bullet Physics 库的 btCollisionWorld C++ 类。我在库的物理模拟部分总体上取得了很好的成功,但我在尝试包装碰撞检测部分时遇到了麻烦。
如果您查看https://github.com/kripken/bullet/blob/master/src/BulletCollision/CollisionDispatch/btCollisionWorld.h,您会发现包含我要包装的 btCollisionWorld 类的 .h 文件。这实际上是一个比我正在使用的版本更新的版本,但我相信它在重要方面是相同的。
(此外,如果您查看https://github.com/bulletphysics/bullet3/blob/master/examples/Raycast/RaytestDemo.cpp,您将看到一个使用该库的示例 C++ 程序。这可能有助于显示我希望在 Cython 中能够做的事情的类型,以及什么Bullet Physics 的普通物理部分我可以做到。)
通常我可以通过 Cython 访问 btCollisionWorld 类的某些部分。例如,如果我将以下内容放入 .pxd 文件中:
cdef cppclass btCollisionWorld:
btBroadphaseInterface* getBroadphase()
然后在我的 Cython 代码中,我可以成功编译并执行如下行:
if cw.getBroadphase():
print "the test passed, we can access a member function in btCollisionWorld"
但是,当我尝试通过使 .pxd 看起来像这样来扩展我的 .pxd 以提供对(例如)“struct LocalShapeInfo”的访问时:
cdef cppclass btCollisionWorld:
btBroadphaseInterface* getBroadphase()
struct LocalShapeInfo:
int m_shapePart
然后尝试使用如下代码从 Cython 访问它:
print "can a member of a struct be accessed?", cw.LocalShapeInfo.m_shapePart
给出这样的错误:
print "can a member of a struct be accessed?", cw.LocalShapeInfo.m_shapePart
^
------------------------------------------------------------
fun6.pyx:1428:53: Object of type 'btCollisionWorld' has no attribute 'LocalShapeInfo'
从上面引用的 .h 文件可以看出,LocalShapeInfo 只是 btCollisionWorld 类中的一个结构。但我无法从 Cython 获得它!
我已经尝试了许多用于在 .pxd 文件中定义结构的语法变体,但似乎没有任何效果。我无法从 CYTHON 访问类内部的结构!!我没有看到任何使用 Cython 访问结构的示例在类中声明。这支持吗?如果是这样,怎么做?我可以访问成员函数和成员变量,但不能访问结构,无论我如何在 .pxd 中声明它们以及如何从 Cython 访问它们。我正在使用 c++ 选项进行编译。
我可能会做一些愚蠢的事情,但我尝试了很多事情。上面的问题实际上只是解决我真正问题的第一个障碍,但由于它看起来非常基本且包含,我想我会先在这里解决它。
顺便说一句,过去我问过如何从一个类中实例化一个 派生 结构。在这个问题中,结构是在类中声明的。
【问题讨论】: