From: James B. <bi...@cs...> - 2006-03-01 18:26:56
|
James Bigler wrote: > I have a struct called Nrrd that has an array of structs: > > typedef struct { > size_t size; > } NrrdAxisInfo; > > typedef struct { > NrrdAxisInfo axis[10; > int dim; > } Nrrd; > > I want to be able to do something like this in python: > > nrrd = nrrdNew() > nrrdLoad(nrrd, "filename.nrrd", None) > width = nrrd.axis[0].size > height = nrrd.axis[1].size > > I looked a little into %array_class, but I'm not sure how to make that > work either. Well, I tried %array_class, but that was really annoying: teem.i: %include "carrays.i" %array_class(NrrdAxisInfo, NrrdAxisInfoArray); From python: >>> NrrdAxisInfoArray.frompointer(nrrd.axis)[0].size 70 >>> NrrdAxisInfoArray.frompointer(nrrd.axis)[1].size 135 The following seemed a better solution. teem.i: %extend NrrdAxisInfo { NrrdAxisInfo* __getitem__(int i) { NrrdAxisInfo* pointer = self; pointer+=i; return pointer; } Now I can do what I want directly: >>> nrrd.axis[0].size 70 >>> nrrd.axis[1].size 135 My question now is, could this construct be automated for several different structs without having to duplicate the code for each one? I'm not very familiar with typemaps and such. Could they be used to do extend several structs at once? Thanks, James |