Registered Member
|
Hello,
I'm using Eigen for programming a lib interacting with a properitery FEM-programm. Therefore all values i/o to the lib is provided by pointers to plain array's. Of course I'm using Eigen::Map< Eigen::Matrix< ... > > to wrapp the plain data to more convienent objects. The lib itself and all its internal functions relies on Eigen::Matrix types. But now I'm faced with the following problem: Functions expecting Eigen::Matrix as arguments do not accept Eigen::Map< Eigen::Matrix< ... > >. If I try to do that, the compiler complains about incompatible types. Even is the object mapped TO is of the same type. I would expect that a function taking a Matrix<double,6,6> would also take Map< Matrix< double, 6,6 > > Please see code below:
Compiler says:
Reading around in the Eigen Docs I found, that the compiler prevents functions to take references on temporary objects. Thus I modifiyed the source following the "const" hack. The code compails with this, but provides wrong output, please see below:
Output:
Please can you provide any help on how to enable functions to take references to Eigen::Matrix and their compatible Eigen::Map< Eigen::Matrix< ... > types. I've experimented with templated functions and derived argument types, but this leads to completly open function interface with out any control about the arguments passed in. Thus I lose all the benefit a type validating language offers. Thus I would at least want to ensure that correct type (float/double) and dimensions are passed in. Thank you for support! |
Registered Member
|
Hello,
after searching the forum again I thing the following solution is the best, see below. I have to template the function and must make sure that each argument is able to get it own derived type to enable the mixture of Eigen::Map and Eigen::Matrix arguments to be passed in from the caller. To make the function header as tight as possible I can use MatrixBase to define that a Matrix must be passed in Dimensions have to be checked after wards in the function body by STATIC_ASSERTS. Thank you for the greate library and that everything need was already in place. May be it would be helpful do extend the tutorial page 10 about this example.
|
Moderator
|
EDIT: oops I did not see you found the solution by yourself, great.
Well you can still control the input types of template functions using static assertion and/or enable_if constructs. Now if you really don't want to have template public code you can still have a simple public template function calling an internal version that take a Map<> object as argument. The template public function will simply convert the user input into a Map<>, eg.: template <typename Derived> void createStiffness(MatrixBase<Derived> &stiffness) { Map<Matrix6x6> tmp(stiffness.data(), stiffness.rows(), stiffness.cols()); createStiffness_impl(tmp); } void createStiffness_impl(Map<Matrix6x6>& stiffness) { ... } You can also generalize this to Map<> objects with a stride such that you can also accept block expressions as input. For the next version we plan to allow to automatically construct Map<> objects from compatible objects such that the above template wrapper won't be needed anymore. Note that storage order will still have to match. |
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]