from ctypes import POINTER, c_double, c_size_t, c_void_p, Structure, cast
import numpy as np
import ipdb
class External(Structure):
_fields_ = [("data", c_void_p),
("length", c_size_t)]
@classmethod
def from_param(cls, seq):
return seq if isinstance(seq, cls) else cls(seq)
def __init__(self, seq):
self.ptr = cast(
np.array(seq, dtype=np.float64).ctypes.data_as(POINTER(c_double)),
c_void_p
)
self.data = self.ptr
self.length = len(seq)
if __name__ == "__main__":
interiors = [
[[3.5, 3.5], [4.4, 2.0], [2.6, 2.0], [3.5, 3.5]],
[[4.0, 3.0], [4.0, 3.2], [4.5, 3.2], [4.0, 3.0]],
]
wrong = [External(s) for s in interiors]
for w in wrong:
shape = w.length, 2
ptr = cast(w.data, POINTER(c_double))
ipdb.set_trace()
array = np.ctypeslib.as_array(ptr, shape)
print "Wrong array", array.tolist()