Skip to content

Commit

Permalink
Fix style in VectorCasters.h file
Browse files Browse the repository at this point in the history
  • Loading branch information
GiulioRomualdi committed Nov 25, 2021
1 parent 0b3b349 commit 7552d12
Showing 1 changed file with 37 additions and 69 deletions.
106 changes: 37 additions & 69 deletions bindings/pybind11/include/iDynTree/pybind11/VectorCasters.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,34 @@ namespace detail

/**
* is_vector_fixsize is used to build a type-dependent expression that check if an element
* has is an iDynTree::VectorFixSize. This specific implementation is used
* when the the object is not an iDynTree::VectorFixSize
* is an iDynTree::VectorFixSize.
*/
template<typename>
struct is_vector_fixsize : std::false_type {};

/**
* is_vector_fixsize is used to build a type-dependent expression that check if an element
* has is an iDynTree::VectorFixSize. This specific implementation is used
* when the the object not an iDynTree::VectorFixSize
*/
template<unsigned int N>
struct is_vector_fixsize<::iDynTree::VectorFixSize<N>> : std::true_type {};

/**
* is_matrix_fixsize is used to build a type-dependent expression that check if an element
* has is an iDynTree::MatrixFixSize. This specific implementation is used
* when the the object is not an iDynTree::MatrixFixSize
* is an iDynTree::MatrixFixSize.
*/
template<typename>
struct is_matrix_fixsize : std::false_type {};

/**
* is_matrix_fixsize is used to build a type-dependent expression that check if an element
* has is an iDynTree::MatrixFixSize. This specific implementation is used
* when the the object is an iDynTree::MatrixFixSize
*/
template<unsigned int nRows, unsigned int nCols>
struct is_matrix_fixsize<::iDynTree::MatrixFixSize<nRows, nCols>> : std::true_type {};

/**
* vector_size_at_compile_time is a utility metafunction to get the compile-time size of an iDynTree
* vector. This specific implementation is used with VectorDynSize.
* vector.
*/
template<typename>
struct vector_size_at_compile_time
{
constexpr static int value = -1;
};

/**
* vector_size_at_compile_time is a utility metafunction to get the compile-time size of an iDynTree
* vector. This specific implementation is used with VectorFixSize.
*/
template<unsigned int N>
struct vector_size_at_compile_time<::iDynTree::VectorFixSize<N>>
{
Expand All @@ -70,7 +54,7 @@ struct vector_size_at_compile_time<::iDynTree::VectorFixSize<N>>

/**
* matrix_size_at_compile_time is a utility metafunction to get the compile-time rows and cols of
* an iDynTree matrix. This specific implementation is used with MatrixDynSize.
* an iDynTree matrix.
*/
template<typename>
struct matrix_size_at_compile_time
Expand All @@ -79,10 +63,6 @@ struct matrix_size_at_compile_time
constexpr static int cols = -1;
};

/**
* matrix_size_at_compile_time is a utility metafunction to get the compile-time rows and cols of
* an iDynTree matrix. This specific implementation is used with MatrixFixSize.
*/
template<unsigned int nRows, unsigned int nCols>
struct matrix_size_at_compile_time<::iDynTree::MatrixFixSize<nRows, nCols>>
{
Expand All @@ -95,23 +75,21 @@ struct matrix_size_at_compile_time<::iDynTree::MatrixFixSize<nRows, nCols>>
/**
* type_caster implementation for the iDynTree Vectors (both fixed and dynamical size)
*/
template <typename Type>
struct type_caster<Type,
enable_if_t<is_vector_fixsize<Type>::value
|| std::is_same<Type, iDynTree::VectorDynSize>::value>>
template <typename VectorType>
struct type_caster<VectorType,
std::enable_if_t<is_vector_fixsize<VectorType>::value
|| std::is_same<VectorType, iDynTree::VectorDynSize>::value>>
{
private:

template <typename U>
static bool checkSize(const std::size_t size, const U& value)
{
static bool checkSize(const std::size_t size, const U& value) {
return true;
}

template <typename U,
typename std::enable_if_t<is_vector_fixsize<U>::value>>
static bool checkSize(const std::size_t size, const U& value)
{
static bool checkSize(const std::size_t size, const U& value) {
return size == value.size();
}

Expand All @@ -121,54 +99,50 @@ struct type_caster<Type,
static constexpr auto descriptor = _("numpy.ndarray[")
+ npy_format_descriptor<double>::name
+ _("[")
+ _<is_vector_fixsize<Type>::value>(_<(size_t) vector_size_at_compile_time<Type>::value>(),
+ _<is_vector_fixsize<VectorType>::value>(_<(size_t) vector_size_at_compile_time<VectorType>::value>(),
_("m"))
+ _(", 1]]");

public:
PYBIND11_TYPE_CASTER(Type, descriptor);
PYBIND11_TYPE_CASTER(VectorType, descriptor);

/**
* Conversion from Python to C++
*/
bool load(pybind11::handle src, bool convert)
{
bool load(pybind11::handle src, bool convert) {
namespace py = ::pybind11;

// If we're in no-convert mode, only load if given an array of the correct type
if (!convert && !py::isinstance<array_t<double>>(src))
{
if (!convert && !py::isinstance<array_t<double>>(src)) {
return false;
}

const auto buf = py::array_t<double, py::array::c_style | py::array::forcecast>::ensure(src);
if (!buf)
if (!buf) {
return false;
}

// It must be a vector
const std::size_t dims = buf.ndim();
if (dims != 1)
{
if (dims != 1) {
return false;
}

// Check if the size of the vector is correct.
if (!this->checkSize(buf.size(), value))
{
if (!this->checkSize(buf.size(), value)) {
return false;
}

// Copy the content of the python vector in to the iDynTree vector
value = Type(buf.data(), buf.size());
value = VectorType(buf.data(), buf.size());

return true;
}

/**
* Conversion from C++ to Python
*/
static handle cast(Type src, pybind11::return_value_policy policy, pybind11::handle parent)
{
static handle cast(VectorType src, pybind11::return_value_policy policy, pybind11::handle parent) {
namespace py = ::pybind11;

// copy the content of the iDynTree vector in the python one
Expand All @@ -180,23 +154,21 @@ struct type_caster<Type,
/**
* type_caster implementation for the iDynTree Matrices (both fixed and dynamical size)
*/
template <typename Type>
struct type_caster<Type,
enable_if_t<is_matrix_fixsize<Type>::value
|| std::is_same<Type, iDynTree::MatrixDynSize>::value>>
template <typename MatrixType>
struct type_caster<MatrixType,
enable_if_t<is_matrix_fixsize<MatrixType>::value
|| std::is_same<MatrixType, iDynTree::MatrixDynSize>::value>>
{
private:

template <typename U>
static bool checkSize(const std::size_t size, const U& value)
{
static bool checkSize(const std::size_t size, const U& value) {
return true;
}

template <typename U,
typename std::enable_if_t<is_matrix_fixsize<U>::value>>
static bool checkSize(const std::size_t size, const U& value)
{
static bool checkSize(const std::size_t size, const U& value) {
return size == value.rows() * value.cols();
}

Expand All @@ -206,44 +178,41 @@ struct type_caster<Type,
static constexpr auto descriptor = _("numpy.ndarray[")
+ npy_format_descriptor<double>::name
+ _("[")
+ _<is_matrix_fixsize<Type>::value>(_<(size_t) matrix_size_at_compile_time<Type>::rows>(),
+ _<is_matrix_fixsize<MatrixType>::value>(_<(size_t) matrix_size_at_compile_time<MatrixType>::rows>(),
_("m"))
+ _(", ")
+ _<is_matrix_fixsize<Type>::value>(_<(size_t) matrix_size_at_compile_time<Type>::rows>(),
+ _<is_matrix_fixsize<MatrixType>::value>(_<(size_t) matrix_size_at_compile_time<MatrixType>::rows>(),
_("n"))
+ _("]]");

public:

PYBIND11_TYPE_CASTER(Type, descriptor);
PYBIND11_TYPE_CASTER(MatrixType, descriptor);

/**
* Conversion from Python to C++
*/
bool load(pybind11::handle src, bool convert)
{
bool load(pybind11::handle src, bool convert) {
namespace py = ::pybind11;

// If we're in no-convert mode, only load if given an array of the correct type
if (!convert && !py::isinstance<array_t<double>>(src))
{
if (!convert && !py::isinstance<array_t<double>>(src)) {
return false;
}

const auto buf = py::array_t<double, py::array::c_style | py::array::forcecast>::ensure(src);
if (!buf)
{
if (!buf) {
return false;
}

// since it is a matrix the number of dimension must be equal to 2
const std::size_t dims = buf.ndim();
if (dims != 2)
if (dims != 2) {
return false;
}

// Check if the size of the vector is correct.
if (!this->checkSize(buf.size(), value))
{
if (!this->checkSize(buf.size(), value)) {
return false;
}

Expand All @@ -252,16 +221,15 @@ struct type_caster<Type,
// order
const std::size_t rows = buf.shape(0);
const std::size_t cols = buf.shape(1);
value = Type(buf.data(), rows, cols);
value = MatrixType(buf.data(), rows, cols);

return true;
}

/**
* Conversion from C++ to Python
*/
static handle cast(Type src, pybind11::return_value_policy policy, pybind11::handle parent)
{
static handle cast(MatrixType src, pybind11::return_value_policy policy, pybind11::handle parent) {
namespace py = ::pybind11;

// Copy the content of the iDynTree matrix into the python one
Expand Down

0 comments on commit 7552d12

Please sign in to comment.