python - SWIG with Eigen3::Vector3d and std::vector<Vector3d> -


the corresponding gist here.


i'd use swig call bunch of c++ function python, functions accept vectors. far, i've implemented std_vector.i , std::vector<double>, since end of converting eigen::vector3d anyways, thought might better make native. small c++ example is

#ifndef mytest_hpp #define mytest_hpp  #include <iostream> #include <eigen/dense>  void print_norm(const eigen::vector3d & x) {   std::cout << x.norm() << std::endl; }  void print_norms(const std::vector<eigen::vector3d> & xs) {   (const auto & x: xs) {       std::cout << x.norm() << std::endl;   } }  #endif // mytest_hpp 

i have no idea though how best call python. perhaps

import mytest  = [1, 1, 0] mytest.print_norm(a) 

this reasonable? numpy.array might work. either way, have no idea put in mytest.i.

any hints?

there few examples of wrapping eigen types numpy floating around on web, biomechanical toolkit implementation copied, , recommend using 1 too. looks relatively big, that's sanity checks , separate templates different types.

conversion numpy eigen works using obj_to_array_contiguous_allow_conversion function accompanying numpy.i, followed pyarray_data data contiguous c(++) array, data assigned each coefficient in eigen matrix separately.

the other way around pretty inverse: python numpy array created pyarray_simplenew, filled data eigen matrix.

it doesn't directly wrapping std::vector<eigen::vector3d>, can set using %include <stl.i> probably, in experience better use nx3 numpy arrays lists of 3d vectors, because of issues eigen, alignment , stl containers.


Comments