-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/312 eigen swig int support #313
Conversation
8f31562
to
757d4f9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm good with these changes. I started to modify existing example script to better make use of this, but it is less trivial than thought. Using getState()
, for example, returns a 3x1 dumpy array, where the EigenVector3d2np()
method returns a 1x3 list. I would leave it as is for now. We can improve with time if we see a need.
Please do add release notes to this change.
@schaubh As far as I know, If anything, the only change users might experience is that function overloading works a bit different now. Because now we provide better
In the past, calling Is this a change worth documenting in the release notes? I'll my opinion about these helper functions in a separate comment. |
If you feel like it, you can updated a few script to use the newer method if you want. I don’t think we have to depreciate the conversion macros yet at this stage. |
Moreover, I've taken a look into the
I would be partial to removing all use of these functions in our codebase and use the numpy functions. New users to the code will immediately recognize what these functions are doing (assuming some experience with However, would you recommend we deprecate them? It might seem pedantic to users to ask them to change their code just because we are not very happy with some utility functions (which we dont really need to remove in the future). EDIT: Following @schaubh 's comment above, I think there is no harm done in leaving the utility functions for those users who prefer to use those. |
The new typemaps support non-double scalar types, overloading, and are stricter on their data validation.
757d4f9
to
91d6af6
Compare
@juan-g-bonilla , you can remove the use of To be frank, we never required people use As branches are committed and we see people using methods that could be improved, we are free to point these out as options to consider. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great PR. I can't give feedback on the SWIG implementation since I'm unfamiliar with it, but I appreciate the introduction of tests of Python code.
Good points. I will undo the last two commits and just remove the use of |
Typemaps can handle the conversion implicitely
91d6af6
to
f1aabb4
Compare
Description
SWIG typemaps are how we tell SWIG to translate C++ objects to Python objects and vice-versa.
swig_eigen.i
is a file that provides these typemaps for Eigen matrices (Eigen::Vector3d
,Eigen::MatrixXd
, etc.). These objects are translated from and to Python lists (both pre this PR and after).This PR, however, makes important changes on how this file is organized.
%fragment
s are used to define C++ functions that implement most of the behaviour, which allow us to somewhat reduce codebloat in SWIG wrappers. Typemaps then use these functions to perform their transformations and checks.In terms of functionality, the most important changes comes from the use of type information to inform transformations. This is, when we want to translate a
MatrixX3i
, we now from this type that the input/output must have size-1x3
and must be integer typed. Using this information, we can perform size checks on inputs (seecheckPyObjectIsMatrixLike
), as well as asserting that the types are correct.Transformation between C and Python types is done in
castPyToC
andcastCToPy
. They make use of the type information of the matrix to select the appropriate Python/C API function (PyLong_FromLong
,PyFloat_AsDouble
, etc.). They also perform sanity checks on the inputs (mainly asserting that integral types are within bounds).In terms of the typemaps themselves, there are some miscellaneous improvements:
matrixAssemble
, usingoptimal="1"
, usingstd::move
, etc.)Eigen::Vector3d&&
)Rotation classes (
MRPd
,Quaterniond
) are handled slightly differently, as they are alwaysdouble
-based and can be set from three different formats (MRP, Quaternion, Rotation Matrix), but are always output as as MRPs. This is legacy behaviour and has remained unchanged. However, they use essentially the same functions as the rest of Eigen types. The main difference is the use ofrotationConversion
, which will intelligently choose an appropriate transformation between any rotation format to another.Verification
A new test suit has been included:
src\architecture\utilitiesSelfCheck\_UnitTest\test_swigEigen.py
. This checks that input-output works as expected, that error messages are thrown as expected, that function overloading work with the intended precedence, and that all qualifier combinations are supported (reference, const reference, const, and rvalues).Documentation
User-facing interfaces are preserved. No changes necessary.
Function overloading*
Now, it is possible to overload functions with different Eigen types, and SWIG will be able to call the "most specific" function. For a python input
[1, 2, 3]
, the following overloads would be called in this order of precedence:The order is therefore: fixed-size before dynamically sized, boolean before int before double, and std before Eigen.
Future work
Expand type maps for boolean types and unsigned integer as they are needed. Should be as easy as adding another
EIGEN_MAT_WRAP
statement toswig_eigen
.