diff --git a/copyable.hpp b/copyable.hpp new file mode 100644 index 0000000000000000000000000000000000000000..d91601bd8eeed7adb6af5ba3efe6346c2b3a778c --- /dev/null +++ b/copyable.hpp @@ -0,0 +1,34 @@ +// +// Copyright (c) 2016-2021 CNRS INRIA +// + +#ifndef __pinocchio_python_utils_copyable_hpp__ +#define __pinocchio_python_utils_copyable_hpp__ + +#include <boost/python.hpp> + +namespace pinocchio +{ + namespace python + { + + namespace bp = boost::python; + + /// + /// \brief Add the Python method copy to allow a copy of this by calling the copy constructor. + /// + template<class C> + struct CopyableVisitor + : public bp::def_visitor< CopyableVisitor<C> > + { + template<class PyClass> + void visit(PyClass & cl) const + { cl.def("copy",©,bp::arg("self"),"Returns a copy of *this."); } + + private: + static C copy(const C & self) { return C(self); } + }; + } // namespace python +} // namespace pinocchio + +#endif // ifndef __pinocchio_python_utils_copyable_hpp__ diff --git a/pickle-vector.hpp b/pickle-vector.hpp new file mode 100644 index 0000000000000000000000000000000000000000..669a24634540e374897db01575efda69b7110ea5 --- /dev/null +++ b/pickle-vector.hpp @@ -0,0 +1,51 @@ +// +// Copyright (c) 2019-2020 CNRS INRIA +// + +#ifndef __pinocchio_python_utils_pickle_vector_hpp__ +#define __pinocchio_python_utils_pickle_vector_hpp__ + +#include <boost/python.hpp> +#include <boost/python/tuple.hpp> +#include <boost/python/stl_iterator.hpp> + +namespace pinocchio +{ + namespace python + { + /// + /// \brief Create a pickle interface for the std::vector and aligned vector + /// + /// \tparam VecType Vector Type to pickle + /// + template<typename VecType> + struct PickleVector : boost::python::pickle_suite + { + static boost::python::tuple getinitargs(const VecType&) + { return boost::python::make_tuple(); } + + static boost::python::tuple getstate(boost::python::object op) + { + return boost::python::make_tuple(boost::python::list(boost::python::extract<const VecType&>(op)())); + } + + static void setstate(boost::python::object op, boost::python::tuple tup) + { + if(boost::python::len(tup) > 0) + { + VecType & o = boost::python::extract<VecType&>(op)(); + boost::python::stl_input_iterator<typename VecType::value_type> begin(tup[0]), end; + while(begin != end) + { + o.push_back(*begin); + ++begin; + } + } + } + + static bool getstate_manages_dict() { return true; } + }; + } +} + +#endif // ifndef __pinocchio_python_utils_pickle_vector_hpp__ diff --git a/std-aligned-vector.hpp b/std-aligned-vector.hpp new file mode 100644 index 0000000000000000000000000000000000000000..13a28243b4bc8299cb892d830673cb6980129739 --- /dev/null +++ b/std-aligned-vector.hpp @@ -0,0 +1,84 @@ +// +// 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/container/aligned-vector.hpp" + +#include "pinocchio/bindings/python/utils/pickle-vector.hpp" +#include "pinocchio/bindings/python/utils/std-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__