Added evaluation of expressions when inserting in Values #1577
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR removes the inconvenient need to evaluate expressions involving values of type
Point2
orPoint3
when passing them toValues::insert
, as mentioned in #1489.In other words, using Class Template Argument Deduction (CTAD) a user can now simply call
insert(key, a + b)
instead ofinsert(key, (a + b).eval())
orinsert<Point2>(key, a + b)
.The related functions
Values::insert_or_assign
andValues::update
have been modified in a similar manner.Unit tests have also been added to verify the functionality.
Details
The inconvenience was originally caused by the fact that
Point2
andPoint3
are really justEigen::Vector2
andEigen::Vector3
, and since Eigen uses lazy evaluation, expressions involving vectors are not automatically evaluated to vectors until they are needed (which is why the use of theauto
keyword is discouraged in many cases).The solution that was implemented here was to add template specializations for values of type
Eigen::CwiseUnaryOp
andEigen::CwiseBinaryOp
. Both are needed so expressions can be chained together, as in2*a + b - c
.Following the guidance for designing functions that take Eigen types as parameters, I tried to implement the following function prototype:
However, this produces a lot of errors related to invalid use of incomplete type
struct Eigen::internal::traits<gtsam::Rot3>
, which I don't currently understand.Related Future Work
There is a related inconvenience involving the creation of factors, as in
BetweenFactor(key1, key2, b - a, noise)
. It would be a bit more difficult to implement implicit evaluation for Eigen types ofa
andb
since the base class ofNoiseModelFactorN
used for most factors only includes the noise model and not the actual measurement (b - a
in this case). Thus, every factor would need to implement template specializations forEigen::CwiseUnaryOp
andEigen::CwiseBinaryOp
, or perhaps they could all inherit from something like aGenericFactor
that would include both the noise model and the measurement.