- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试围绕一些现有的 C code 构建一个 python 接口(interface)与CFFI。与往常一样,C 代码为了性能而进行了修剪,它充满了大量的宏和类型定义。
ATM 我正在努力复制以下结构
#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
通过上面的内容,我在 ptr_vertices = ffi.new("REAL *[3 ]", 顶点)
调用时:
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 元素数组。我希望有人能在这里为我指出正确的方向。
最佳答案
如果您要创建单个项目,请使用 ffi.new('REAL(*)[3]', [1, 2, 3])
。 *
两边的括号很重要。
在 C 中,类型 REAL(*)[3]
表示指向 REAL 数组 (size=3) 的指针,而 REAL*[3]
表示指向实数的指针数组(大小=3)。请参阅C pointer to array/array of pointers disambiguation详细信息。
现在,您正在创建一个项目数组,CFFI expects an array type相反,as 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
关于python - 在 CFFI 中创建类型为 `REAL (* vertices)[DIM]` 的 cdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937895/
好的,我尝试了这种从外国翻译过来的方法,它确实有效我在我的库中的 structs.lisp 文件中定义了这些,它在我所有其他依赖项之前首先加载 (cffi:defcstruct (cv-size :c
我正在尝试使用来自 SBCL 的 BLAS/LAPACK 库(特别是试图让 LLA 包运行)。我在加载 BLAS 共享库时遇到了很多麻烦;最终我发现它无法加载其依赖库。最终我能够通过手动加载它的所有依
有一种方法可以用字典初始化结构: fooData= {'y': 1, 'x': 2} fooStruct = ffi.new("foo_t*", fooData) fooBuffer = ffi.bu
我想使用 stat(2)来自 Common Lisp。 我已经定义了 stat 函数使用的结构: (cffi:defctype mode_t :unsigned-int) (cffi:defctype
在普通的 lisp 中,是否有推荐的外来类型的命名约定? (和一般类型)?例如: (cffi:defctype glyph-index-t :uint32) (cffi:defcstruct Poin
假设我在 cffi 下定义了一个枚举: from cffi import FFI ffi = FFI() ffi.cdef('typedef enum {RANDOM, IMMEDIATE, SEAR
IplImage struct documentation描述了 IplROI* roi 槽,它似乎是指向核心 types_c.h 头文件中定义的 IplROI struct 的指针: typede
我使用 cffi 模块来包装一个简单的 C 代码。问题是,我需要添加一个标志以使其编译 (std=c99)。目前我有类似的东西: from cffi import FFI ffibuilder = F
这是一个 C 语言的函数: union Example { int number; void *pointer; }; void return_a_value (union Examp
我是 Common Lisp 的新手,并对其进行了一些实验。我正在努力获得对 Windows 剪贴板的一些访问权限,然后我找到了这个引用: https://groups.google.com/foru
我实际上正在编写一个带有 cffi 模块的 python 程序来测试我的 C/ASM 库,我设法让它工作。但我不知道如何访问字符串中间字符的地址以将其传递给我的 lib 函数。例如: def my_b
我的最终目标是编译系统 FLEXI-TRIVIAL-DIRED ( http://common-lisp.net/project/ftd/ ),但我遇到了麻烦,因为我在任何地方都找不到所需的软件包之一
如果我使用 cffi.FFI.new 创建一个新结构,如何将其传递给具有相同结构定义的不同 FFI 中的函数? 我有一个基本的 C 结构,我通过 cffi 包在 Python 中使用它,我想将其传递给
我正在尝试使用 pip3 在 fedora23 工作站上安装 cffi。但我收到以下错误。 pip3 install cffi Collecting cffi Using cached cffi-
我想使用 cffi(如果必须,甚至使用 ctypes)从 Linux 上的 Python 3 访问 C ABI。该 API 由许多 .so 文件实现(我们称它们为 libA.so、libB.so 和
我想公开一个对象的缓冲区协议(protocol),就像in this example Cython 文档,但是我需要使用 CFFI 来执行此操作而且我找不到任何公开缓冲区协议(protocol)的示例
我正在学习如何将用 C 编写的代码包含到 Python 中,因为我有一个用于 Microchip 设备的 API,这对于我希望通过为其添加一个 Python 包装器来让我将来的生活更轻松,这将使我能够
我的C++代码是 StructureEx* obj; // structure functionEx((void**)&obj); 我的职能是 int functionEx(void** obj);
尝试在我的 linux 机器上的 python 3.4.3 上安装 cffi-1.7.0 源代码。出现以下错误。 $ python setup.py 安装 cc1:错误:无法识别的命令行选项“-Wno
我有一些 C 库,我想使用 CFFI 在 Python 中访问。构建库后,我得到了 2 个文件:$HOME/libcint/include/cint.h 和 $HOME/libcint/lib/lib
我是一名优秀的程序员,十分优秀!