Skip to content

Some question about c struct array to numpy array with swig #9

Description

@xichaoqiang

Hello, sir

in order to speed up the compuation of python, I chose to use matlab's coder toolbox to convert matlab to c/c++ code, so as not to rely on matlab runtime.

I just recently learned

swig through coder-swig.View coder-swig on File Exchange

This package converts wrap MATLAB Coder generated C and C++ code to python code with swig.

But I found it is too complicated to use the function in the c dll file with numpy , as shown in the code example.

I have many questions about the i files about swig,Can you help me?

Here is some code changed from the third example of the coder-swig package.
03-timestwo-dynamic-size.zip

Firsr, the Code generation for function 'timestwo' is shown as below.

/* Function Definitions in timestwo.h*/
void timestwo(const emxArray_real_T *x, emxArray_real_T *y)

Here x is input array ,y is output array.

the emxArray is a struct is show as below.

/* Type Definitions in timestwo_types.h*/
#ifndef struct_emxArray_real_T
#define struct_emxArray_real_T

struct emxArray_real_T
{
  double *data;
  int *size;
  int allocatedSize;
  int numDimensions;
  boolean_T canFreeData;
};

#endif    

I have write an i file to wrap emxArray_real_T for numpy array.

It seems not work.Can you help me to check it?

Or is there a simple or clever way to convert emx array to numpy array, like convert eigen matirx to numpy wieth eigen.i file?

%module timestwo
%{
#define SWIG_FILE_WITH_INIT
#include "codegen/dll/timestwo/timestwo_types.h"
#include "codegen/dll/timestwo/timestwo.h"
#include "codegen/dll/timestwo/timestwo_emxAPI.h"
#include "codegen/dll/timestwo/timestwo_initialize.h"
#include "codegen/dll/timestwo/timestwo_terminate.h"
%}
%include "carrays.i"
%array_class(double, doubleArray);
//%array_class(int, intArray);

%include "numpy.i"
%fragment("NumPy_Fragments");
%init %{
import_array();
%}

//%apply (double* IN_ARRAY2, int DIM1, int DIM2) {(double *data, int rows, int cols)};

/* Generate includes for required headers */

#include "codegen/dll/timestwo/timestwo_types.h"
#include "codegen/dll/timestwo/timestwo.h"
#include "codegen/dll/timestwo/timestwo_emxAPI.h"
#include "codegen/dll/timestwo/timestwo_initialize.h"
#include "codegen/dll/timestwo/timestwo_terminate.h"

/* numpy array as input */
%typemap(in)
    (emxArray_real_T*)
    (PyArrayObject* array=NULL, int is_new_object=0)
{
    array = (PyArrayObject*) obj_to_array_contiguous_allow_conversion($input, NPY_DOUBLE, &is_new_object);
    if (!array) SWIG_fail;
    int numDimensions;
	numDimensions=array_numdims(array)	;
	int*size= (int*)malloc(numDimensions * sizeof(int));
	int n_elems=1;
	for (int i=0;i<numDimensions;i++)
	{
	size[i]=array_size(array, i);
	n_elems*=size[i];	
	}
    $1 = emxCreateND_real_T(numDimensions, size);
    memcpy($1->data, array_data(array), n_elems);
}

/* numpy array as output */
%typemap(out) 
(emxArray_real_T*)
{
    $result = NULL;
    if ($1)
    {	
            int numDimensions= $1->numDimensions;
            npy_intp dims =  $1->size ;
            PyObject* res_array = PyArray_SimpleNewFromData(numDimensions, dims, NPY_DOUBLE,$1->data);
            $result = SWIG_Python_AppendOutput($result, res_array);
            emxDestroyArray_real_T($1);
    }

    if($result == NULL)
    {
        PyErr_SetString(PyExc_RuntimeError, "Result not yet implemented");
    }
}


%typemap(freearg)
    (emxArray_real_T*)
{
  if (is_new_object$argnum && array$argnum) Py_DECREF(array$argnum);
  emxDestroyArray_real_T($1);
}



/* Destructor for emxArray_real_T  */
%extend emxArray_real_T {
    ~emxArray_real_T() {
        emxDestroyArray_real_T($self);
    }
}

/* Tell SWIG about allocators */
//%newobject emxCreate_real_T;
//%newobject emxCreateWrapper_real_T;
//%newobject emxCreateND_real_T;
//%newobject emxCreateWrapperND_real_T;

/* Parse necessary headers */
%include codegen/dll/timestwo/timestwo_types.h
%include codegen/dll/timestwo/timestwo.h
%include codegen/dll/timestwo/timestwo_emxAPI.h
%include codegen/dll/timestwo/timestwo_initialize.h
%include codegen/dll/timestwo/timestwo_terminate.h

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions