py_vector.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include <Eigen/Dense>
  2. #include <Eigen/Sparse>
  3. #include "python.h"
  4. template <typename Type> void init_fixed_from_buffer_3(Type &v, py::buffer &b) {
  5. typedef typename Type::Scalar Scalar;
  6. py::buffer_info info = b.request();
  7. if (info.format != py::format_descriptor<Scalar>::value())
  8. throw std::runtime_error("Incompatible buffer format!");
  9. if (!((info.ndim == 1 && info.strides[0] == sizeof(Scalar)) ||
  10. (info.ndim == 2 &&
  11. ((info.shape[0] == 1 && info.strides[0] == sizeof(Scalar) &&
  12. info.shape[1] == 3) ||
  13. (info.shape[1] == 1 && info.strides[1] == sizeof(Scalar) &&
  14. info.shape[0] == 3)))))
  15. throw std::runtime_error("Incompatible buffer dimension!");
  16. memcpy(v.data(), info.ptr, sizeof(Scalar) * 3);
  17. }
  18. /// Creates Python bindings for an Eigen order-1 tensor of size 3 (i.e. a vector/normal/point)
  19. template <typename Type>
  20. py::class_<Type> bind_eigen_1_3(py::module &m, const char *name,
  21. py::object parent = py::object()) {
  22. typedef typename Type::Scalar Scalar;
  23. py::class_<Type> vector(m, name, parent);
  24. vector
  25. /* Constructors */
  26. .def(py::init<>())
  27. .def(py::init<Scalar>())
  28. .def(py::init<Scalar, Scalar, Scalar>())
  29. .def("__init__", [](Type &v, const std::vector<Scalar> &v2) {
  30. if (v2.size() != 3)
  31. throw std::runtime_error("Incompatible size!");
  32. memcpy(v.data(), &v2[0], sizeof(Scalar) * 3);
  33. })
  34. .def("__init__", [](Type &v, py::buffer b) {
  35. init_fixed_from_buffer_3(v, b);
  36. })
  37. /* Initialization */
  38. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  39. .def("setZero", [](Type &m) { m.setZero(); })
  40. /* Arithmetic operators (def_cast forcefully casts the result back to a
  41. Matrix to avoid type issues with Eigen's crazy expression templates) */
  42. .def_cast(-py::self)
  43. .def_cast(py::self + py::self)
  44. .def_cast(py::self - py::self)
  45. .def_cast(py::self * Scalar())
  46. .def_cast(py::self / Scalar())
  47. .def_cast(py::self += py::self)
  48. .def_cast(py::self -= py::self)
  49. .def_cast(py::self *= Scalar())
  50. .def_cast(py::self /= Scalar())
  51. /* Comparison operators */
  52. .def(py::self == py::self)
  53. .def(py::self != py::self)
  54. /* Python protocol implementations */
  55. .def("__len__", [](const Type &) { return (int) 3; })
  56. .def("__repr__", [](const Type &v) {
  57. std::ostringstream oss;
  58. oss << v;
  59. return oss.str();
  60. })
  61. .def("__getitem__", [](const Type &c, int i) {
  62. if (i < 0 || i >= 3)
  63. throw py::index_error();
  64. return c[i];
  65. })
  66. .def("__setitem__", [](Type &c, int i, Scalar v) {
  67. if (i < 0 || i >= 3)
  68. throw py::index_error();
  69. c[i] = v;
  70. })
  71. /* Buffer access for interacting with NumPy */
  72. .def_buffer([](Type &m) -> py::buffer_info {
  73. return py::buffer_info(
  74. m.data(), /* Pointer to buffer */
  75. sizeof(Scalar), /* Size of one scalar */
  76. /* Python struct-style format descriptor */
  77. py::format_descriptor<Scalar>::value(),
  78. 1, { (size_t) 3 },
  79. { sizeof(Scalar) }
  80. );
  81. });
  82. return vector;
  83. }
  84. /// Creates Python bindings for a dynamic Eigen order-1 tensor (i.e. a vector)
  85. template <typename Type>
  86. py::class_<Type> bind_eigen_1(py::module &m, const char *name,
  87. py::object parent = py::object()) {
  88. typedef typename Type::Scalar Scalar;
  89. /* Many Eigen functions are templated and can't easily be referenced using
  90. a function pointer, thus a big portion of the binding code below
  91. instantiates Eigen code using small anonymous wrapper functions */
  92. py::class_<Type> vector(m, name, parent);
  93. vector
  94. /* Constructors */
  95. .def(py::init<>())
  96. .def(py::init<size_t>())
  97. .def("__init__", [](Type &v, const std::vector<Scalar> &v2) {
  98. new (&v) Type(v2.size());
  99. memcpy(v.data(), &v2[0], sizeof(Scalar) * v2.size());
  100. })
  101. .def("__init__", [](Type &v, py::buffer b) {
  102. py::buffer_info info = b.request();
  103. if (info.format != py::format_descriptor<Scalar>::value()) {
  104. throw std::runtime_error("Incompatible buffer format!");
  105. } else if (info.ndim == 1 && info.strides[0] == sizeof(Scalar)) {
  106. new (&v) Type(info.shape[0]);
  107. memcpy(v.data(), info.ptr, sizeof(Scalar) * info.shape[0]);
  108. } else if (info.ndim == 2 && ((info.shape[0] == 1 && info.strides[0] == sizeof(Scalar))
  109. || (info.shape[1] == 1 && info.strides[1] == sizeof(Scalar)))) {
  110. new (&v) Type(info.shape[0] * info.shape[1]);
  111. memcpy(v.data(), info.ptr, sizeof(Scalar) * info.shape[0] * info.shape[1]);
  112. } else {
  113. throw std::runtime_error("Incompatible buffer dimension!");
  114. }
  115. })
  116. /* Size query functions */
  117. .def("size", [](const Type &m) { return m.size(); })
  118. .def("cols", &Type::cols)
  119. .def("rows", &Type::rows)
  120. /* Initialization */
  121. .def("setZero", [](Type &m) { m.setZero(); })
  122. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  123. /* Resizing */
  124. .def("resize", [](Type &m, size_t s0) { m.resize(s0); })
  125. .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  126. .def("conservativeResize", [](Type &m, size_t s0) { m.conservativeResize(s0); })
  127. /* Component-wise operations */
  128. .def("cwiseAbs", &Type::cwiseAbs)
  129. .def("cwiseAbs2", &Type::cwiseAbs2)
  130. .def("cwiseSqrt", &Type::cwiseSqrt)
  131. .def("cwiseInverse", &Type::cwiseInverse)
  132. .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  133. .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  134. .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  135. .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  136. .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  137. .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  138. /* Arithmetic operators (def_cast forcefully casts the result back to a
  139. Type to avoid type issues with Eigen's crazy expression templates) */
  140. .def_cast(-py::self)
  141. .def_cast(py::self + py::self)
  142. .def_cast(py::self - py::self)
  143. .def_cast(py::self * Scalar())
  144. .def_cast(py::self / Scalar())
  145. /* Arithmetic in-place operators */
  146. .def_cast(py::self += py::self)
  147. .def_cast(py::self -= py::self)
  148. .def_cast(py::self *= py::self)
  149. .def_cast(py::self *= Scalar())
  150. .def_cast(py::self /= Scalar())
  151. /* Comparison operators */
  152. .def(py::self == py::self)
  153. .def(py::self != py::self)
  154. /* Python protocol implementations */
  155. .def("__repr__", [](const Type &v) {
  156. std::ostringstream oss;
  157. oss << v.transpose();
  158. return oss.str();
  159. })
  160. .def("__getitem__", [](const Type &m, size_t i) {
  161. if (i >= (size_t) m.size())
  162. throw py::index_error();
  163. return m[i];
  164. })
  165. .def("__setitem__", [](Type &m, size_t i, Scalar v) {
  166. if (i >= (size_t) m.size())
  167. throw py::index_error();
  168. m[i] = v;
  169. })
  170. /* Buffer access for interacting with NumPy */
  171. .def_buffer([](Type &m) -> py::buffer_info {
  172. return py::buffer_info(
  173. m.data(), /* Pointer to buffer */
  174. sizeof(Scalar), /* Size of one scalar */
  175. /* Python struct-style format descriptor */
  176. py::format_descriptor<Scalar>::value(),
  177. 1, /* Number of dimensions */
  178. { (size_t) m.size() }, /* Buffer dimensions */
  179. { sizeof(Scalar) } /* Strides (in bytes) for each index */
  180. );
  181. })
  182. /* Static initializers */
  183. .def_static("Zero", [](size_t n) { return Type(Type::Zero(n)); })
  184. .def_static("Ones", [](size_t n) { return Type(Type::Ones(n)); })
  185. .def_static("Constant", [](size_t n, Scalar value) { return Type(Type::Constant(n, value)); });
  186. return vector;
  187. }
  188. /// Creates Python bindings for a dynamic Eigen order-2 tensor (i.e. a matrix)
  189. template <typename Type>
  190. py::class_<Type> bind_eigen_2(py::module &m, const char *name,
  191. py::object parent = py::object()) {
  192. typedef typename Type::Scalar Scalar;
  193. /* Many Eigen functions are templated and can't easily be referenced using
  194. a function pointer, thus a big portion of the binding code below
  195. instantiates Eigen code using small anonymous wrapper functions */
  196. py::class_<Type> matrix(m, name, parent);
  197. matrix
  198. /* Constructors */
  199. .def(py::init<>())
  200. .def(py::init<size_t, size_t>())
  201. .def("__init__", [](Type &m, Scalar f) {
  202. new (&m) Type(1, 1);
  203. m(0, 0) = f;
  204. })
  205. .def("__init__", [](Type &m, py::buffer b) {
  206. py::buffer_info info = b.request();
  207. if (info.format != py::format_descriptor<Scalar>::value())
  208. throw std::runtime_error("Incompatible buffer format!");
  209. if (info.ndim == 1) {
  210. new (&m) Type(info.shape[0], 1);
  211. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  212. } else if (info.ndim == 2) {
  213. if (info.strides[0] == sizeof(Scalar)) {
  214. new (&m) Type(info.shape[0], info.shape[1]);
  215. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  216. } else {
  217. new (&m) Type(info.shape[1], info.shape[0]);
  218. memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  219. m.transposeInPlace();
  220. }
  221. } else {
  222. throw std::runtime_error("Incompatible buffer dimension!");
  223. }
  224. })
  225. /* Size query functions */
  226. .def("size", [](const Type &m) { return m.size(); })
  227. .def("cols", &Type::cols)
  228. .def("rows", &Type::rows)
  229. /* Initialization */
  230. .def("setZero", [](Type &m) { m.setZero(); })
  231. .def("setIdentity", [](Type &m) { m.setIdentity(); })
  232. .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  233. /* Resizing */
  234. .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
  235. .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  236. .def("conservativeResize", [](Type &m, size_t s0, size_t s1) { m.conservativeResize(s0, s1); })
  237. /* Component-wise operations */
  238. .def("cwiseAbs", &Type::cwiseAbs)
  239. .def("cwiseAbs2", &Type::cwiseAbs2)
  240. .def("cwiseSqrt", &Type::cwiseSqrt)
  241. .def("cwiseInverse", &Type::cwiseInverse)
  242. .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  243. .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  244. .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  245. .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  246. .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  247. .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  248. /* Arithmetic operators (def_cast forcefully casts the result back to a
  249. Type to avoid type issues with Eigen's crazy expression templates) */
  250. .def_cast(-py::self)
  251. .def_cast(py::self + py::self)
  252. .def_cast(py::self - py::self)
  253. .def_cast(py::self * py::self)
  254. .def_cast(py::self * Scalar())
  255. .def_cast(py::self / Scalar())
  256. /* Arithmetic in-place operators */
  257. .def_cast(py::self += py::self)
  258. .def_cast(py::self -= py::self)
  259. .def_cast(py::self *= py::self)
  260. .def_cast(py::self *= Scalar())
  261. .def_cast(py::self /= Scalar())
  262. /* Comparison operators */
  263. .def(py::self == py::self)
  264. .def(py::self != py::self)
  265. .def("transposeInPlace", [](Type &m) { m.transposeInPlace(); })
  266. /* Other transformations */
  267. .def("transpose", [](Type &m) -> Type { return m.transpose(); })
  268. /* Python protocol implementations */
  269. .def("__repr__", [](const Type &v) {
  270. std::ostringstream oss;
  271. oss << v;
  272. return oss.str();
  273. })
  274. .def("__getitem__", [](const Type &m, std::pair<size_t, size_t> i) {
  275. if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  276. throw py::index_error();
  277. return m(i.first, i.second);
  278. })
  279. .def("__setitem__", [](Type &m, std::pair<size_t, size_t> i, Scalar v) {
  280. if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  281. throw py::index_error();
  282. m(i.first, i.second) = v;
  283. })
  284. /* Buffer access for interacting with NumPy */
  285. .def_buffer([](Type &m) -> py::buffer_info {
  286. return py::buffer_info(
  287. m.data(), /* Pointer to buffer */
  288. sizeof(Scalar), /* Size of one scalar */
  289. /* Python struct-style format descriptor */
  290. py::format_descriptor<Scalar>::value(),
  291. 2, /* Number of dimensions */
  292. { (size_t) m.rows(), /* Buffer dimensions */
  293. (size_t) m.cols() },
  294. { sizeof(Scalar), /* Strides (in bytes) for each index */
  295. sizeof(Scalar) * m.rows() }
  296. );
  297. })
  298. /* Static initializers */
  299. .def_static("Zero", [](size_t n, size_t m) { return Type(Type::Zero(n, m)); })
  300. .def_static("Ones", [](size_t n, size_t m) { return Type(Type::Ones(n, m)); })
  301. .def_static("Constant", [](size_t n, size_t m, Scalar value) { return Type(Type::Constant(n, m, value)); })
  302. .def_static("Identity", [](size_t n, size_t m) { return Type(Type::Identity(n, m)); });
  303. return matrix;
  304. }
  305. /// Creates Python bindings for a dynamic Eigen sparse order-2 tensor (i.e. a matrix)
  306. template <typename Type>
  307. py::class_<Type> bind_eigen_sparse_2(py::module &m, const char *name,
  308. py::object parent = py::object()) {
  309. typedef typename Type::Scalar Scalar;
  310. /* Many Eigen functions are templated and can't easily be referenced using
  311. a function pointer, thus a big portion of the binding code below
  312. instantiates Eigen code using small anonymous wrapper functions */
  313. py::class_<Type> matrix(m, name, parent);
  314. matrix
  315. /* Constructors */
  316. .def(py::init<>())
  317. .def(py::init<size_t, size_t>())
  318. // .def("__init__", [](Type &m, Scalar f) {
  319. // new (&m) Type(1, 1);
  320. // m(0, 0) = f;
  321. // })
  322. // .def("__init__", [](Type &m, py::buffer b) {
  323. // py::buffer_info info = b.request();
  324. // if (info.format != py::format_descriptor<Scalar>::value())
  325. // throw std::runtime_error("Incompatible buffer format!");
  326. // if (info.ndim == 1) {
  327. // new (&m) Type(info.shape[0], 1);
  328. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  329. // } else if (info.ndim == 2) {
  330. // if (info.strides[0] == sizeof(Scalar)) {
  331. // new (&m) Type(info.shape[0], info.shape[1]);
  332. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  333. // } else {
  334. // new (&m) Type(info.shape[1], info.shape[0]);
  335. // memcpy(m.data(), info.ptr, sizeof(Scalar) * m.size());
  336. // m.transposeInPlace();
  337. // }
  338. // } else {
  339. // throw std::runtime_error("Incompatible buffer dimension!");
  340. // }
  341. // })
  342. /* Size query functions */
  343. .def("size", [](const Type &m) { return m.size(); })
  344. .def("cols", [](const Type &m) { return m.cols(); })
  345. .def("rows", [](const Type &m) { return m.rows(); })
  346. /* Initialization */
  347. // .def("setZero", [](Type &m) { m.setZero(); })
  348. // .def("setIdentity", [](Type &m) { m.setIdentity(); })
  349. // .def("setConstant", [](Type &m, Scalar value) { m.setConstant(value); })
  350. /* Resizing */
  351. // .def("resize", [](Type &m, size_t s0, size_t s1) { m.resize(s0, s1); })
  352. // .def("resizeLike", [](Type &m, const Type &m2) { m.resizeLike(m2); })
  353. // .def("conservativeResize", [](Type &m, size_t s0, size_t s1) { m.conservativeResize(s0, s1); })
  354. /* Component-wise operations */
  355. // .def("cwiseAbs", &Type::cwiseAbs)
  356. // .def("cwiseAbs2", &Type::cwiseAbs2)
  357. // .def("cwiseSqrt", &Type::cwiseSqrt)
  358. // .def("cwiseInverse", &Type::cwiseInverse)
  359. // .def("cwiseMin", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMin(m2); })
  360. // .def("cwiseMax", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseMax(m2); })
  361. // .def("cwiseMin", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMin(s); })
  362. // .def("cwiseMax", [](const Type &m1, Scalar s) -> Type { return m1.cwiseMax(s); })
  363. // .def("cwiseProduct", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseProduct(m2); })
  364. // .def("cwiseQuotient", [](const Type &m1, const Type &m2) -> Type { return m1.cwiseQuotient(m2); })
  365. /* Arithmetic operators (def_cast forcefully casts the result back to a
  366. Type to avoid type issues with Eigen's crazy expression templates) */
  367. .def_cast(-py::self)
  368. .def_cast(py::self + py::self)
  369. .def_cast(py::self - py::self)
  370. .def_cast(py::self * py::self)
  371. .def_cast(py::self * Scalar())
  372. .def(py::self * Eigen::Matrix<Scalar,Eigen::Dynamic,Eigen::Dynamic>())
  373. .def_cast(py::self / Scalar())
  374. /* Arithmetic in-place operators */
  375. // .def_cast(py::self += py::self)
  376. // .def_cast(py::self -= py::self)
  377. // .def_cast(py::self *= py::self)
  378. // .def_cast(py::self *= Scalar())
  379. // .def_cast(py::self /= Scalar())
  380. /* Comparison operators */
  381. // .def(py::self == py::self)
  382. // .def(py::self != py::self)
  383. // .def("transposeInPlace", [](Type &m) { m.transposeInPlace(); })
  384. // /* Other transformations */
  385. // .def("transpose", [](Type &m) -> Type { return m.transpose(); })
  386. /* Python protocol implementations */
  387. .def("__repr__", [](const Type &v) {
  388. std::ostringstream oss;
  389. oss << v;
  390. return oss.str();
  391. })
  392. // .def("__getitem__", [](const Type &m, std::pair<size_t, size_t> i) {
  393. // if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  394. // throw py::index_error();
  395. // return m(i.first, i.second);
  396. // })
  397. // .def("__setitem__", [](Type &m, std::pair<size_t, size_t> i, Scalar v) {
  398. // if (i.first >= (size_t) m.rows() || i.second >= (size_t) m.cols())
  399. // throw py::index_error();
  400. // m(i.first, i.second) = v;
  401. // })
  402. // /* Buffer access for interacting with NumPy */
  403. // .def_buffer([](Type &m) -> py::buffer_info {
  404. // return py::buffer_info(
  405. // m.data(), /* Pointer to buffer */
  406. // sizeof(Scalar), /* Size of one scalar */
  407. // /* Python struct-style format descriptor */
  408. // py::format_descriptor<Scalar>::value(),
  409. // 2, /* Number of dimensions */
  410. // { (size_t) m.rows(), /* Buffer dimensions */
  411. // (size_t) m.cols() },
  412. // { sizeof(Scalar), /* Strides (in bytes) for each index */
  413. // sizeof(Scalar) * m.rows() }
  414. // );
  415. // })
  416. /* Static initializers */
  417. // .def_static("Zero", [](size_t n, size_t m) { return Type(Type::Zero(n, m)); })
  418. // .def_static("Ones", [](size_t n, size_t m) { return Type(Type::Ones(n, m)); })
  419. // .def_static("Constant", [](size_t n, size_t m, Scalar value) { return Type(Type::Constant(n, m, value)); })
  420. // .def_static("Identity", [](size_t n, size_t m) { return Type(Type::Identity(n, m)); })
  421. ;
  422. return matrix;
  423. }
  424. void python_export_vector(py::module &m) {
  425. py::module me = m.def_submodule(
  426. "eigen", "Wrappers for Eigen types");
  427. /* Bindings for VectorXd */
  428. bind_eigen_1<Eigen::VectorXd> (me, "VectorXd");
  429. py::implicitly_convertible<py::buffer, Eigen::VectorXd>();
  430. py::implicitly_convertible<double, Eigen::VectorXd>();
  431. /* Bindings for VectorXi */
  432. bind_eigen_1<Eigen::VectorXi> (me, "VectorXi");
  433. py::implicitly_convertible<py::buffer, Eigen::VectorXi>();
  434. py::implicitly_convertible<double, Eigen::VectorXi>();
  435. /* Bindings for MatrixXd */
  436. bind_eigen_2<Eigen::MatrixXd> (me, "MatrixXd");
  437. py::implicitly_convertible<py::buffer, Eigen::MatrixXd>();
  438. py::implicitly_convertible<double, Eigen::MatrixXd>();
  439. /* Bindings for MatrixXi */
  440. bind_eigen_2<Eigen::MatrixXi> (me, "MatrixXi");
  441. py::implicitly_convertible<py::buffer, Eigen::MatrixXi>();
  442. py::implicitly_convertible<double, Eigen::MatrixXi>();
  443. /* Bindings for Vector3d */
  444. auto vector3 = bind_eigen_1_3<Eigen::Vector3d>(me, "Vector3d");
  445. vector3
  446. .def("norm", [](const Eigen::Vector3d &v) { return v.norm(); })
  447. .def("squaredNorm", [](const Eigen::Vector3d &v) { return v.squaredNorm(); })
  448. .def("normalize", [](Eigen::Vector3d &v) { v.normalize(); })
  449. .def("normalized", [](const Eigen::Vector3d &v) -> Eigen::Vector3d { return v.normalized(); })
  450. .def("dot", [](const Eigen::Vector3d &v1, const Eigen::Vector3d &v2) { return v1.dot(v2); })
  451. .def("cross", [](const Eigen::Vector3d &v1, const Eigen::Vector3d &v2) -> Eigen::Vector3d { return v1.cross(v2); })
  452. .def_property("x", [](const Eigen::Vector3d &v) -> double { return v.x(); },
  453. [](Eigen::Vector3d &v, double x) { v.x() = x; }, "X coordinate")
  454. .def_property("y", [](const Eigen::Vector3d &v) -> double { return v.y(); },
  455. [](Eigen::Vector3d &v, double y) { v.y() = y; }, "Y coordinate")
  456. .def_property("z", [](const Eigen::Vector3d &v) -> double { return v.z(); },
  457. [](Eigen::Vector3d &v, double z) { v.z() = z; }, "Z coordinate");
  458. py::implicitly_convertible<py::buffer, Eigen::Vector3d>();
  459. py::implicitly_convertible<double, Eigen::Vector3d>();
  460. /* Bindings for SparseMatrix<double> */
  461. bind_eigen_sparse_2< Eigen::SparseMatrix<double> > (me, "SparseMatrixd");
  462. /* Bindings for SparseMatrix<int> */
  463. bind_eigen_sparse_2< Eigen::SparseMatrix<int> > (me, "SparseMatrixi");
  464. }