Accessing/wrapping Cython 中 class 中的结构
Accessing/wrapping a struct inside a class in Cython
我正在尝试用 Cython 包装 Bullet Physics 库的 btCollisionWorld C++ class。我在库的物理模拟部分通常取得了很好的成功,但我在尝试包装碰撞检测部分时遇到了 运行 麻烦。
如果您查看 https://github.com/kripken/bullet/blob/master/src/BulletCollision/CollisionDispatch/btCollisionWorld.h,您会发现包含我试图包装的 btCollisionWorld class 的 .h 文件。这实际上比我正在使用的版本更新一些,但我相信它在重要方面是相同的。
(此外,如果您查看 https://github.com/bulletphysics/bullet3/blob/master/examples/Raycast/RaytestDemo.cpp,您将看到一个使用该库的示例 C++ 程序。这可能有助于显示我希望能够在Cython,以及我可以为 Bullet Physics 的普通物理部分做些什么。)
通常我可以通过 Cython 访问 btCollisionWorld class 的某些部分。例如,如果我将以下内容放入我的 .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 中的一个结构 class。但是我无法从 Cython 获取它!
我尝试了多种语法变体来定义 .pxd 文件中的结构,但似乎没有任何效果。我无法从 CYTHON 访问 类 内部的结构!!我没有看到任何使用 Cython 访问 在 classes 中声明的结构的示例。这受支持吗?如果是这样,如何?我可以访问成员函数和成员变量,但不能访问结构,无论我如何在 .pxd 中声明它们以及如何从 Cython 访问它们。我正在使用 C++ 选项进行编译。
我可能在做一些愚蠢的事情,但我尝试了很多事情。上面的问题实际上只是解决我的实际问题的第一个障碍,但由于它看起来很基本并且包含在内,所以我想我会先在这里解决它。
顺便说一句,过去我问过关于从 class 实例化结构 derived 的问题。在这个问题中,结构是 在 class.
中声明的
我认为你只是没能区分类型名称和成员名称。这样的事情对我有用(至少通过了 cython 阶段):
cdef extern from *:
cdef cppclass btCollisionWorld:
struct LocalShapeInfo:
int m_shapePart
LocalShapeInfo m_shape
cdef f(btCollisionWorld& r):
return r.m_shape.m_shapePart
我正在尝试用 Cython 包装 Bullet Physics 库的 btCollisionWorld C++ class。我在库的物理模拟部分通常取得了很好的成功,但我在尝试包装碰撞检测部分时遇到了 运行 麻烦。
如果您查看 https://github.com/kripken/bullet/blob/master/src/BulletCollision/CollisionDispatch/btCollisionWorld.h,您会发现包含我试图包装的 btCollisionWorld class 的 .h 文件。这实际上比我正在使用的版本更新一些,但我相信它在重要方面是相同的。
(此外,如果您查看 https://github.com/bulletphysics/bullet3/blob/master/examples/Raycast/RaytestDemo.cpp,您将看到一个使用该库的示例 C++ 程序。这可能有助于显示我希望能够在Cython,以及我可以为 Bullet Physics 的普通物理部分做些什么。)
通常我可以通过 Cython 访问 btCollisionWorld class 的某些部分。例如,如果我将以下内容放入我的 .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 中的一个结构 class。但是我无法从 Cython 获取它!
我尝试了多种语法变体来定义 .pxd 文件中的结构,但似乎没有任何效果。我无法从 CYTHON 访问 类 内部的结构!!我没有看到任何使用 Cython 访问 在 classes 中声明的结构的示例。这受支持吗?如果是这样,如何?我可以访问成员函数和成员变量,但不能访问结构,无论我如何在 .pxd 中声明它们以及如何从 Cython 访问它们。我正在使用 C++ 选项进行编译。
我可能在做一些愚蠢的事情,但我尝试了很多事情。上面的问题实际上只是解决我的实际问题的第一个障碍,但由于它看起来很基本并且包含在内,所以我想我会先在这里解决它。
顺便说一句,过去我问过关于从 class 实例化结构 derived 的问题。在这个问题中,结构是 在 class.
中声明的我认为你只是没能区分类型名称和成员名称。这样的事情对我有用(至少通过了 cython 阶段):
cdef extern from *:
cdef cppclass btCollisionWorld:
struct LocalShapeInfo:
int m_shapePart
LocalShapeInfo m_shape
cdef f(btCollisionWorld& r):
return r.m_shape.m_shapePart