py_vector.cpp 17 KB

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