Skip to content
Merged
Show file tree
Hide file tree
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
35 changes: 20 additions & 15 deletions include/eigenpy/eigen-allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ namespace eigenpy
}
}
};

template<typename MatType>
bool check_swap(PyArrayObject * pyArray,
const Eigen::MatrixBase<MatType> & mat)
{
if(PyArray_NDIM(pyArray) == 0) return false;
if(mat.rows() == PyArray_DIMS(pyArray)[0])
return false;
else
return true;
}

template<typename Scalar, typename NewScalar, bool cast_is_valid = FromTypeToType<Scalar,NewScalar>::value >
struct cast_matrix_or_array
Expand All @@ -75,10 +86,7 @@ namespace eigenpy
const Eigen::MatrixBase<MatrixOut> & dest)
{
MatrixOut & dest_ = const_cast<MatrixOut &>(dest.derived());
if(dest.rows() == input.rows())
dest_ = input.template cast<NewScalar>();
else
dest_ = input.transpose().template cast<NewScalar>();
dest_ = input.template cast<NewScalar>();
}
};

Expand All @@ -97,11 +105,11 @@ namespace eigenpy
} // namespace details

#define EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX(MatType,Scalar,NewScalar,pyArray,mat) \
details::cast_matrix_or_array<Scalar,NewScalar>::run(NumpyMap<MatType,Scalar>::map(pyArray),mat)
details::cast_matrix_or_array<Scalar,NewScalar>::run(NumpyMap<MatType,Scalar>::map(pyArray,details::check_swap(pyArray,mat)),mat)

#define EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY(MatType,Scalar,NewScalar,mat,pyArray) \
details::cast_matrix_or_array<Scalar,NewScalar>::run(mat,NumpyMap<MatType,NewScalar>::map(pyArray))
details::cast_matrix_or_array<Scalar,NewScalar>::run(mat,NumpyMap<MatType,NewScalar>::map(pyArray,details::check_swap(pyArray,mat)))

template<typename MatType>
struct EigenAllocator
{
Expand All @@ -119,7 +127,7 @@ namespace eigenpy
const int Scalar_type_code = Register::getTypeCode<Scalar>();
if(pyArray_type_code == Scalar_type_code)
{
mat = NumpyMap<MatType,Scalar>::map(pyArray); // avoid useless cast
mat = NumpyMap<MatType,Scalar>::map(pyArray,details::check_swap(pyArray,mat)); // avoid useless cast
return;
}

Expand Down Expand Up @@ -167,11 +175,8 @@ namespace eigenpy

if(pyArray_type_code == Scalar_type_code) // no cast needed
{
MapType map_pyArray = NumpyMap<MatType,Scalar>::map(pyArray);
if(mat.rows() == map_pyArray.rows())
map_pyArray = mat;
else
map_pyArray = mat.transpose();
MapType map_pyArray = NumpyMap<MatType,Scalar>::map(pyArray,details::check_swap(pyArray,mat));
map_pyArray = mat;
return;
}

Expand Down Expand Up @@ -252,7 +257,7 @@ namespace eigenpy
RefType & mat = *reinterpret_cast<RefType*>(raw_ptr);
if(pyArray_type_code == Scalar_type_code)
{
mat = NumpyMap<MatType,Scalar>::map(pyArray); // avoid useless cast
mat = NumpyMap<MatType,Scalar>::map(pyArray,details::check_swap(pyArray,mat)); // avoid useless cast
return;
}

Expand Down Expand Up @@ -346,7 +351,7 @@ namespace eigenpy
MatType & mat = *mat_ptr;
if(pyArray_type_code == Scalar_type_code)
{
mat = NumpyMap<MatType,Scalar>::map(pyArray); // avoid useless cast
mat = NumpyMap<MatType,Scalar>::map(pyArray,details::check_swap(pyArray,mat)); // avoid useless cast
return;
}

Expand Down
2 changes: 2 additions & 0 deletions include/eigenpy/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#define EIGENPY_NO_ALIGNMENT_VALUE Eigen::Unaligned

#define EIGENPY_UNUSED_VARIABLE(var) (void)(var)

#include "eigenpy/expose.hpp"

namespace eigenpy
Expand Down
32 changes: 22 additions & 10 deletions include/eigenpy/numpy-map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace eigenpy
typedef NumpyMapTraits<MatType, InputScalar, AlignmentValue, Stride> Impl;
typedef typename Impl::EigenMap EigenMap;

static EigenMap map(PyArrayObject* pyArray);
static EigenMap map(PyArrayObject* pyArray, bool swap_dimensions = false);
};

} // namespace eigenpy
Expand All @@ -39,7 +39,7 @@ namespace eigenpy
typedef Eigen::Matrix<InputScalar,MatType::RowsAtCompileTime,MatType::ColsAtCompileTime,MatType::Options> EquivalentInputMatrixType;
typedef Eigen::Map<EquivalentInputMatrixType,AlignmentValue,Stride> EigenMap;

static EigenMap mapImpl(PyArrayObject* pyArray)
static EigenMap mapImpl(PyArrayObject* pyArray, bool swap_dimensions = false)
{
enum {
OuterStrideAtCompileTime = Stride::OuterStrideAtCompileTime,
Expand Down Expand Up @@ -77,11 +77,22 @@ namespace eigenpy
assert( (PyArray_DIMS(pyArray)[0] < INT_MAX)
&& (PyArray_STRIDE(pyArray,0) < INT_MAX));

rows = (int)PyArray_DIMS(pyArray)[0];
cols = 1;

inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
outer_stride = 0;
if(!swap_dimensions)
{
rows = (int)PyArray_DIMS(pyArray)[0];
cols = 1;

inner_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
outer_stride = 0;
}
else
{
rows = 1;
cols = (int)PyArray_DIMS(pyArray)[0];

inner_stride = 0;
outer_stride = (int)PyArray_STRIDE(pyArray, 0) / (int)itemsize;
}
}

// Specific care for Eigen::Stride<-1,0>
Expand Down Expand Up @@ -113,8 +124,9 @@ namespace eigenpy
typedef Eigen::Matrix<InputScalar,MatType::RowsAtCompileTime,MatType::ColsAtCompileTime,MatType::Options> EquivalentInputMatrixType;
typedef Eigen::Map<EquivalentInputMatrixType,AlignmentValue,Stride> EigenMap;

static EigenMap mapImpl(PyArrayObject* pyArray)
static EigenMap mapImpl(PyArrayObject* pyArray, bool swap_dimensions = false)
{
EIGENPY_UNUSED_VARIABLE(swap_dimensions);
assert( PyArray_NDIM(pyArray) <= 2 );

int rowMajor;
Expand All @@ -141,9 +153,9 @@ namespace eigenpy

template<typename MatType, typename InputScalar, int AlignmentValue, typename Stride>
typename NumpyMap<MatType,InputScalar,AlignmentValue,Stride>::EigenMap
NumpyMap<MatType,InputScalar,AlignmentValue,Stride>::map(PyArrayObject * pyArray)
NumpyMap<MatType,InputScalar,AlignmentValue,Stride>::map(PyArrayObject * pyArray, bool swap_dimensions)
{
return Impl::mapImpl(pyArray);
return Impl::mapImpl(pyArray,swap_dimensions);
}

} // namespace eigenpy
Expand Down
12 changes: 6 additions & 6 deletions include/eigenpy/user-type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,12 @@ namespace eigenpy
PyArray_DotFunc * dotfunc = &internal::SpecialMethods<Scalar>::dotfunc;
// PyArray_CastFunc * cast = &internal::SpecialMethods<Scalar>::cast;

int code = Register::registerNewType(py_type_ptr,
&typeid(Scalar),
sizeof(Scalar),
getitem, setitem, nonzero,
copyswap, copyswapn,
dotfunc);
int code = Register::registerNewType(py_type_ptr,
&typeid(Scalar),
sizeof(Scalar),
getitem, setitem, nonzero,
copyswap, copyswapn,
dotfunc);

return code;
}
Expand Down
5 changes: 5 additions & 0 deletions unittest/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,13 @@ BOOST_PYTHON_MODULE(matrix)
namespace bp = boost::python;
eigenpy::enableEigenPy();

// Square matrix
typedef Eigen::Matrix<double,6,6> Matrix6;
eigenpy::enableEigenPySpecific<Matrix6>();

// Non-square matrix
typedef Eigen::Matrix<double,4,6> Matrix46;
eigenpy::enableEigenPySpecific<Matrix46>();

Eigen::MatrixXd (*naturalsXX)(int,int,bool) = naturals;
Eigen::VectorXd (*naturalsX)(int,bool) = naturals;
Expand Down