Skip to content
Snippets Groups Projects
Commit a51d8fb8 authored by Wilson Jallet's avatar Wilson Jallet :clapper:
Browse files

Expose some std::vector of Eigen Vector & Matrix

* add test_std_vector.py
* fix copyable.hpp's include guards & namespace
* same for pickle-vector
* expose std::vector for VectorXd, MatrixXd, VectorXi, MatrixXi
parent d902b083
No related branches found
No related tags found
No related merge requests found
......@@ -155,7 +155,6 @@ set(${PROJECT_NAME}_HEADERS
include/eigenpy/user-type.hpp
include/eigenpy/ufunc.hpp
include/eigenpy/register.hpp
include/eigenpy/std-aligned-vector.hpp
include/eigenpy/std-map.hpp
include/eigenpy/std-vector.hpp
include/eigenpy/pickle-vector.hpp
......@@ -199,6 +198,7 @@ set(${PROJECT_NAME}_SOURCES
src/angle-axis.cpp
src/quaternion.cpp
src/geometry-conversion.cpp
src/std-vector.cpp
src/version.cpp)
add_library(${PROJECT_NAME} SHARED ${${PROJECT_NAME}_SOURCES}
......
......@@ -2,13 +2,12 @@
// Copyright (c) 2016-2021 CNRS INRIA
//
#ifndef __pinocchio_python_utils_copyable_hpp__
#define __pinocchio_python_utils_copyable_hpp__
#ifndef __eigenpy_utils_copyable_hpp__
#define __eigenpy_utils_copyable_hpp__
#include <boost/python.hpp>
namespace pinocchio {
namespace python {
namespace eigenpy {
namespace bp = boost::python;
......@@ -26,7 +25,6 @@ struct CopyableVisitor : public bp::def_visitor<CopyableVisitor<C> > {
private:
static C copy(const C& self) { return C(self); }
};
} // namespace python
} // namespace pinocchio
} // namespace eigenpy
#endif // ifndef __pinocchio_python_utils_copyable_hpp__
#endif // ifndef __eigenpy_utils_copyable_hpp__
......@@ -2,15 +2,14 @@
// Copyright (c) 2019-2020 CNRS INRIA
//
#ifndef __pinocchio_python_utils_pickle_vector_hpp__
#define __pinocchio_python_utils_pickle_vector_hpp__
#ifndef __eigenpy_utils_pickle_vector_hpp__
#define __eigenpy_utils_pickle_vector_hpp__
#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>
#include <boost/python/tuple.hpp>
namespace pinocchio {
namespace python {
namespace eigenpy {
///
/// \brief Create a pickle interface for the std::vector
///
......@@ -42,7 +41,6 @@ struct PickleVector : boost::python::pickle_suite {
static bool getstate_manages_dict() { return true; }
};
} // namespace python
} // namespace pinocchio
} // namespace eigenpy
#endif // ifndef __pinocchio_python_utils_pickle_vector_hpp__
#endif // ifndef __eigenpy_utils_pickle_vector_hpp__
//
// Copyright (c) 2016-2022 CNRS INRIA
//
#ifndef __pinocchio_python_utils_std_aligned_vector_hpp__
#define __pinocchio_python_utils_std_aligned_vector_hpp__
#include <boost/python.hpp>
#include <string>
#include "pinocchio/bindings/python/utils/pickle-vector.hpp"
#include "pinocchio/bindings/python/utils/std-vector.hpp"
#include "pinocchio/container/aligned-vector.hpp"
namespace pinocchio {
namespace python {
///
/// \brief Expose an container::aligned_vector from a type given as template
/// argument.
///
/// \tparam T Type to expose as container::aligned_vector<T>.
/// \tparam EnableFromPythonListConverter Enables the conversion from a Python
/// list to a container::aligned_vector<T>.
///
/// \sa StdAlignedVectorPythonVisitor
///
template <class T, bool NoProxy = false,
bool EnableFromPythonListConverter = true>
struct StdAlignedVectorPythonVisitor
: public ::boost::python::vector_indexing_suite<
typename container::aligned_vector<T>, NoProxy,
internal::contains_vector_derived_policies<
typename container::aligned_vector<T>, NoProxy> >,
public StdContainerFromPythonList<container::aligned_vector<T> > {
typedef container::aligned_vector<T> vector_type;
typedef StdContainerFromPythonList<vector_type, NoProxy>
FromPythonListConverter;
typedef T value_type;
static void expose(const std::string &class_name,
const std::string &doc_string = "") {
expose(class_name, doc_string, EmptyPythonVisitor());
}
template <typename VisitorDerived>
static void expose(
const std::string &class_name,
const boost::python::def_visitor<VisitorDerived> &visitor) {
expose(class_name, "", visitor);
}
template <typename VisitorDerived>
static void expose(
const std::string &class_name, const std::string &doc_string,
const boost::python::def_visitor<VisitorDerived> &visitor) {
namespace bp = boost::python;
if (!register_symbolic_link_to_registered_type<vector_type>()) {
bp::class_<vector_type> cl(class_name.c_str(), doc_string.c_str());
cl.def(StdAlignedVectorPythonVisitor())
.def(bp::init<size_t, const value_type &>(
bp::args("self", "size", "value"),
"Constructor from a given size and a given value."))
.def(bp::init<const vector_type &>(bp::args("self", "other"),
"Copy constructor"))
.def("tolist", &FromPythonListConverter::tolist, bp::arg("self"),
"Returns the aligned_vector as a Python list.")
.def(visitor)
#ifndef PINOCCHIO_PYTHON_NO_SERIALIZATION
.def_pickle(PickleVector<vector_type>())
#endif
.def(CopyableVisitor<vector_type>());
// Register conversion
if (EnableFromPythonListConverter)
FromPythonListConverter::register_converter();
}
}
};
} // namespace python
} // namespace pinocchio
#endif // ifndef __pinocchio_python_utils_std_aligned_vector_hpp__
This diff is collapsed.
......@@ -9,6 +9,7 @@
#include "eigenpy/decompositions/decompositions.hpp"
#include "eigenpy/eigenpy.hpp"
#include "eigenpy/geometry.hpp"
#include "eigenpy/std-vector.hpp"
#include "eigenpy/solvers/preconditioners.hpp"
#include "eigenpy/solvers/solvers.hpp"
#include "eigenpy/utils/is-approx.hpp"
......@@ -30,6 +31,7 @@ BOOST_PYTHON_MODULE(eigenpy_pywrap) {
exposeAngleAxis();
exposeQuaternion();
exposeGeometryConversion();
exposeStdVector();
exposeComputationInfo();
......
/*
* Copyright 2022, CNRS
* Copyright 2022, INRIA
*/
#include "eigenpy/std-vector.hpp"
namespace eigenpy {
template <typename MatType>
void exposeStdVectorEigenSpecificType(const char* name) {
std::string full_name = "StdVec_";
full_name += name;
StdVectorPythonVisitor<std::vector<MatType>, true>::expose(full_name.c_str());
}
void exposeStdVector() {
exposeStdVectorEigenSpecificType<Eigen::MatrixXd>("MatrixXd");
exposeStdVectorEigenSpecificType<Eigen::VectorXd>("VectorXd");
exposeStdVectorEigenSpecificType<Eigen::MatrixXi>("MatrixXi");
exposeStdVectorEigenSpecificType<Eigen::VectorXi>("VectorXi");
}
} // namespace eigenpy
import numpy as np
import eigenpy
x0 = np.random.randn(3)
l1 = [x0, x0, x0]
l2 = eigenpy.StdVec_VectorXd(3, x0)
def checkAllValues(li1, li2):
assert len(li1) == len(li2)
n = len(li1)
for i in range(n):
assert np.array_equal(li1[i], li2[i])
checkAllValues(l1, l2)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment