eigen.cpp
Go to the documentation of this file.
1 #include "eigenpy/fwd.hpp"
2 #include <boost/numpy.hpp>
3 
4 
5 namespace boopy
6 {
7  namespace bpn = boost::numpy;
8  namespace bp = boost::python;
9 
11  {
12  static PyObject* convert(Eigen::VectorXd const& v)
13  {
14  Py_intptr_t shape[1] = { v.size() };
15  bpn::matrix result(bpn::zeros(1, shape, bpn::dtype::get_builtin<double>()));
16  std::copy(v.data(), v.data()+v.size(), reinterpret_cast<double*>(result.get_data()));
17  return bp::incref(result.ptr());
18  }
19  };
20 
22  {
24  {
25  bp::converter::registry
26  ::push_back(&convertible,
27  &construct,
28  bp::type_id<Eigen::VectorXd>());
29  }
30 
31  // Determine if obj_ptr can be converted in a Eigenvec
32  static void* convertible(PyObject* obj_ptr)
33  {
34 
35  try {
36  bp::object obj(bp::handle<>(bp::borrowed(obj_ptr)));
37  std::auto_ptr<bpn::ndarray>
38  array(new bpn::ndarray(bpn::from_object(obj,
39  bpn::dtype::get_builtin<double>(),
40  bpn::ndarray::V_CONTIGUOUS)));
41 
42  if( (array->get_nd()==1)
43  || ( (array->get_nd()==2) && (array->get_shape()[1]==1) ))
44  return array.release();
45  else
46  return 0;
47  } catch (bp::error_already_set & err) {
48  bp::handle_exception();
49  return 0;
50  }
51  }
52 
53  // Convert obj_ptr into a Eigenvec
54  static void construct(PyObject* ,
55  bp::converter::rvalue_from_python_stage1_data* memory)
56  {
57  // Recover the pointer created in <convertible>
58  std::auto_ptr<bpn::ndarray>
59  array(reinterpret_cast<bpn::ndarray*>(memory->convertible));
60  const int nrow = array->get_shape()[0];
61  std::cout << "nrow = " << nrow << std::endl;
62 
63  // Get the memory where to create the vector
64  void* storage
65  = ((bp::converter::rvalue_from_python_storage<Eigen::VectorXd>*)memory)
66  ->storage.bytes;
67 
68  // Create the vector
69  Eigen::VectorXd & res = * new (storage) Eigen::VectorXd(nrow);
70 
71  // Copy the data
72  double * data = (double*)array->get_data();
73  for(int i=0;i<nrow;++i)
74  res[i] = data[i];
75 
76  // Stash the memory chunk pointer for later use by boost.python
77  memory->convertible = storage;
78  }
79  };
80 }
81 
82 Eigen::VectorXd test()
83 {
84  Eigen::VectorXd v = Eigen::VectorXd::Random(5);
85  std::cout << v.transpose() << std::endl;
86  return v;
87 }
88 
89 void test2( Eigen::VectorXd v )
90 {
91  std::cout << "test2: dim = " << v.size() << " ||| v[0] = " << v[0] << std::endl;
92 }
93 
95 {
96  namespace bpn = boost::numpy;
97  namespace bp = boost::python;
98 
99  bpn::initialize();
100  bp::to_python_converter<Eigen::VectorXd,
103 
104  bp::def("test", test);
105  bp::def("test2", test2);
106 }
boost::python::object matrix()
Definition: bnpy.cpp:20
Eigen::VectorXd test()
Definition: eigen.cpp:82
void test2(Eigen::VectorXd v)
Definition: eigen.cpp:89
bn::ndarray array()
Definition: bnpy.cpp:10
data
Definition: setup.in.py:38
BOOST_PYTHON_MODULE(libeigen)
Definition: eigen.cpp:94
static void construct(PyObject *, bp::converter::rvalue_from_python_stage1_data *memory)
Definition: eigen.cpp:54
static PyObject * convert(Eigen::VectorXd const &v)
Definition: eigen.cpp:12
Definition: eigen.cpp:5
static void * convertible(PyObject *obj_ptr)
Definition: eigen.cpp:32


eigenpy
Author(s): Justin Carpentier, Nicolas Mansard
autogenerated on Sat Apr 17 2021 02:37:59