Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xichaoqiang opened this issue Sep 24, 2020 · 0 comments
Open

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

xichaoqiang opened this issue Sep 24, 2020 · 0 comments

Comments

@xichaoqiang
Copy link

xichaoqiang commented Sep 24, 2020

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant