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

Added support for SWIG/Eigen type conversion for int & float matrices. #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions Utilities/SWIG/eigen.i
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/*
* The Biomechanical ToolKit
* Copyright (c) 2009-2014, Arnaud Barré
* All rights reserved.
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
Expand All @@ -18,7 +18,7 @@
* of its contributors may be used to endorse or promote products
* derived from this software without specific prior written
* permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Expand All @@ -32,7 +32,7 @@
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

%{
#define SWIG_FILE_WITH_INIT
#include <Eigen/Core>
Expand All @@ -47,8 +47,8 @@

%fragment("Eigen_Fragments", "header", fragment="NumPy_Fragments")
%{
template <typename T> int NumPyType() {return -1;};
template <typename T> int NumPyType() {return -1;}

template <class Derived>
void ConvertFromNumpyToEigenMatrix(Eigen::MatrixBase<Derived>* out, PyObject* in)
{
Expand Down Expand Up @@ -115,7 +115,7 @@
for (int i = 0; i != rows; ++i)
for (int j = 0; j != cols; ++j)
out->coeffRef(i,j) = data[i*cols+j];
};
}

// Copies values from Eigen type into an existing NumPy type
template <class Derived>
Expand Down Expand Up @@ -176,16 +176,16 @@
PyErr_SetString(PyExc_ValueError, "Impossible to convert the input into a Python array object.");
return;
}

typename Derived::Scalar* data = static_cast<typename Derived::Scalar*>(PyArray_DATA(temp));

for (int i = 0; i != in->rows(); ++i) {
for (int j = 0; j != in->cols(); ++j) {
data[i*in->cols()+j] = in->coeff(i,j);
}
}
};
}

template <class Derived>
void ConvertFromEigenToNumPyMatrix(PyObject** out, Eigen::MatrixBase<Derived>* in)
{
Expand All @@ -195,9 +195,11 @@
for (int i = 0; i != dims[0]; ++i)
for (int j = 0; j != dims[1]; ++j)
data[i*dims[1]+j] = in->coeff(i,j);
};
}

template<> int NumPyType<double>() {return NPY_DOUBLE;};
template<> int NumPyType<int>() {return NPY_INT32;}
template<> int NumPyType<float>() {return NPY_FLOAT;}
template<> int NumPyType<double>() {return NPY_DOUBLE;}
%}

// ----------------------------------------------------------------------------
Expand Down