gpt4 book ai didi

python - 如何将指针传递给指向 python 结构的指针数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:00:33 25 4
gpt4 key购买 nike

我在 Python 中使用 ctypes,我需要将一个指针传递给指向某个 C 函数的结构的指针数组。这是结构:

typedef struct {
float x;
float y;
float z;
float radius;
} Sphere;

我有以下原型(prototype)的功能:

void render(Sphere** spheres);

在 Python 中,我为 Sphere 结构声明了一个类,我需要将 argtypes 设置为 render 函数:

lib_render = ctypes.cdll.LoadLibrary('librender.so')

class Sphere(ctypes.Structure):
_fields_ = [('x', ctypes.c_float),
('y', ctypes.c_float),
('z', ctypes.c_float),
('radius', ctypes.c_float)]

render = lib_render.render
render.argtypes = [<cannot find out what needs to be here>]

spheres = numpy.array([Sphere(1, 2.8, 3, 0.5),
Sphere(4.2, 2, 1, 3.2)])
render(spheres)

如何正确传递该数组?

最佳答案

我不经常使用 numpy,但没有它,下面的内容也能正常工作。我假设如果您将指针传递给指针,则指针列表必须以 null 结尾。

from ctypes import *

class Sphere(Structure):
_fields_ = [('x', c_float),
('y', c_float),
('z', c_float),
('radius', c_float)]

dll = CDLL('test')
dll.render.argtypes = POINTER(POINTER(Sphere)),
dll.render.restype = None

# Create a couple of objects
a = Sphere(1,2,3,4)
b = Sphere(5,6,7,8)

# build a list of pointers, null-terminated.
c = (POINTER(Sphere) * 3)(pointer(a),pointer(b),None)
dll.render(c)

测试动态链接库:

#include <stdio.h>

typedef struct Sphere {
float x;
float y;
float z;
float radius;
} Sphere;

__declspec(dllexport) void render(Sphere** spheres)
{
for(;*spheres;++spheres)
printf("%f %f %f %f\n",(*spheres)->x,(*spheres)->y,(*spheres)->z,(*spheres)->radius);
}

输出:

1.000000 2.000000 3.000000 4.000000
5.000000 6.000000 7.000000 8.000000

使用 numpy,使用 void render(Sphere* spheres, size_t len),这有效。也许更熟悉 numpy 的人可以评论是否可以支持 Sphere**

from ctypes import *
import numpy as np

class Sphere(Structure):
_fields_ = [('x', c_float),
('y', c_float),
('z', c_float),
('radius', c_float)]

dll = CDLL('test')
dll.render.argtypes = POINTER(Sphere),c_size_t
dll.render.restype = None

a = Sphere(1,2,3,4)
b = Sphere(5,6,7,8)
# c = (Sphere * 2)(a,b)
# dll.render(c,len(c))
d = np.array([a,b])
dll.render(d.ctypes.data_as(POINTER(Sphere)),len(d))

关于python - 如何将指针传递给指向 python 结构的指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47086852/

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