Skip to content

Commit

Permalink
Merge pull request #151 from jcarpent/devel
Browse files Browse the repository at this point in the history
Fix issues when compiling in Release mode
  • Loading branch information
jcarpent authored Jan 30, 2020
2 parents d86ce43 + 4ba344d commit a6af570
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 116 deletions.
124 changes: 31 additions & 93 deletions include/eigenpy/details.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,95 +17,6 @@
#include "eigenpy/map.hpp"
#include "eigenpy/exception.hpp"

namespace boost { namespace python { namespace detail {

template<class MatType>
struct referent_size<Eigen::MatrixBase<MatType>&>
{
BOOST_STATIC_CONSTANT(
std::size_t, value = sizeof(MatType));
};

template<class MatType>
struct referent_size<Eigen::EigenBase<MatType>&>
{
BOOST_STATIC_CONSTANT(
std::size_t, value = sizeof(MatType));
};

}}}

namespace boost { namespace python { namespace converter {

template<class MatType>
struct implicit<Eigen::MatrixBase<MatType>,MatType>
{
typedef Eigen::MatrixBase<MatType> Source;
typedef MatType Target;

static void* convertible(PyObject* obj)
{
// Find a converter which can produce a Source instance from
// obj. The user has told us that Source can be converted to
// Target, and instantiating construct() below, ensures that
// at compile-time.
return implicit_rvalue_convertible_from_python(obj, registered<Source>::converters)
? obj : 0;
}

static void construct(PyObject* obj, rvalue_from_python_stage1_data* data)
{
void* storage = ((rvalue_from_python_storage<Target>*)data)->storage.bytes;

arg_from_python<Source> get_source(obj);
bool convertible = get_source.convertible();
BOOST_VERIFY(convertible);

new (storage) Target(get_source().derived());

// record successful construction
data->convertible = storage;
}
};

template<class MatType>
struct implicit<MatType,Eigen::MatrixBase<MatType> >
{
typedef MatType Source;
typedef Eigen::MatrixBase<MatType> Target;

static void* convertible(PyObject* obj)
{
// Find a converter which can produce a Source instance from
// obj. The user has told us that Source can be converted to
// Target, and instantiating construct() below, ensures that
// at compile-time.
return implicit_rvalue_convertible_from_python(obj, registered<Source>::converters)
? obj : 0;
}

static void construct(PyObject* obj, rvalue_from_python_stage1_data* data)
{
void* storage = reinterpret_cast<rvalue_from_python_storage<Target>*>
(reinterpret_cast<void*>(data))->storage.bytes;

arg_from_python<Source> get_source(obj);
bool convertible = get_source.convertible();
BOOST_VERIFY(convertible);

new (storage) Source(get_source());

// record successful construction
data->convertible = storage;
}
};

template<class MatType>
struct implicit<MatType,Eigen::EigenBase<MatType> > : implicit<MatType,Eigen::MatrixBase<MatType> >
{};

}}} // namespace boost::python::converter

#define GET_PY_ARRAY_TYPE(array) PyArray_ObjectType(reinterpret_cast<PyObject *>(array), 0)

namespace eigenpy
Expand Down Expand Up @@ -692,12 +603,39 @@ namespace eigenpy

// Add also conversion to Eigen::MatrixBase<MatType>
typedef Eigen::MatrixBase<MatType> MatrixBase;
// bp::implicitly_convertible<MatTypeBase,MatType>();
bp::implicitly_convertible<MatType,MatrixBase>();

EigenFromPy<MatrixBase>::registration();

// Add also conversion to Eigen::EigenBase<MatType>
typedef Eigen::EigenBase<MatType> EigenBase;
bp::implicitly_convertible<MatType,EigenBase>();
EigenFromPy<EigenBase>::registration();
}
};

template<typename MatType>
struct EigenFromPy< Eigen::MatrixBase<MatType> > : EigenFromPy<MatType>
{
typedef EigenFromPy<MatType> EigenFromPyDerived;
typedef Eigen::MatrixBase<MatType> Base;

static void registration()
{
bp::converter::registry::push_back
(reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible),
&EigenFromPy::construct,bp::type_id<Base>());
}
};

template<typename MatType>
struct EigenFromPy< Eigen::EigenBase<MatType> > : EigenFromPy<MatType>
{
typedef EigenFromPy<MatType> EigenFromPyDerived;
typedef Eigen::EigenBase<MatType> Base;

static void registration()
{
bp::converter::registry::push_back
(reinterpret_cast<void *(*)(_object *)>(&EigenFromPy::convertible),
&EigenFromPy::construct,bp::type_id<Base>());
}
};

Expand Down
14 changes: 10 additions & 4 deletions include/eigenpy/utils/is-approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
namespace eigenpy
{
template<typename MatrixType1, typename MatrixType2>
inline bool is_approx(const Eigen::MatrixBase<MatrixType1> & mat1,
const Eigen::MatrixBase<MatrixType2> & mat2,
const typename MatrixType1::Scalar & prec = Eigen::NumTraits<typename MatrixType1::Scalar>::dummy_precision())
inline EIGEN_DONT_INLINE bool is_approx(const MatrixType1 & mat1,
const MatrixType2 & mat2,
const typename MatrixType1::Scalar & prec)
{
return mat1.isApprox(mat2,prec);
return mat1.derived().isApprox(mat2.derived(),prec);
}

template<typename MatrixType1, typename MatrixType2>
inline bool is_approx(const MatrixType1 & mat1, const MatrixType2 & mat2)
{
return is_approx(mat1,mat2,Eigen::NumTraits<typename MatrixType1::Scalar>::dummy_precision());
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<package format="2">
<name>eigenpy</name>
<version>1.6.13</version>
<version>2.0.0</version>
<description>Bindings between Numpy and Eigen using Boost.Python</description>
<maintainer email="[email protected]">Justin Carpentier</maintainer>
<maintainer email="[email protected]">Wolfgang Merkt</maintainer>
Expand Down
27 changes: 9 additions & 18 deletions python/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,8 @@

#include <boost/python/scope.hpp>

#define DEFINE_IS_APPROX(MatType) \
BOOST_PYTHON_FUNCTION_OVERLOADS(is_approx_overload##MatType,eigenpy::is_approx,2,3)

#define EXPOSE_IS_APPROX(MatType) \
bp::def("is_approx", \
(bool (*)(const Eigen::MatrixBase<MatType> &, \
const Eigen::MatrixBase<MatType> &, \
const MatType::Scalar &))eigenpy::is_approx<MatType,MatType>, \
is_approx_overload##MatType(bp::args("A","B","prec"), \
"Returns True if A is approximately equal to B, within the precision determined by prec."))


using namespace eigenpy;

DEFINE_IS_APPROX(MatrixXd)
DEFINE_IS_APPROX(MatrixXf)


BOOST_PYTHON_MODULE(eigenpy)
{
namespace bp = boost::python;
Expand Down Expand Up @@ -62,8 +46,15 @@ BOOST_PYTHON_MODULE(eigenpy)

{
using namespace Eigen;
EXPOSE_IS_APPROX(MatrixXd);
EXPOSE_IS_APPROX(MatrixXf);
bp::def("is_approx",(bool (*)(const MatrixXd &, const MatrixXd &, const double &))&is_approx<MatrixXd,MatrixXd>,
bp::args("A","B","prec"),
"Returns True if A is approximately equal to B, within the precision determined by prec.");
bp::def("is_approx",(bool (*)(const MatrixXd &, const MatrixXd &))&is_approx<MatrixXd,MatrixXd>,
bp::args("A","B"),
"Returns True if A is approximately equal to B..");

// EXPOSE_IS_APPROX(MatrixXd);
// EXPOSE_IS_APPROX(MatrixXf);
}

exposeDecompositions();
Expand Down

0 comments on commit a6af570

Please sign in to comment.