template<typename PlainObjectType>
class drake::EigenPtr< PlainObjectType >
This wrapper class provides a way to write non-template functions taking raw pointers to Eigen objects as parameters while limiting the number of copies, similar to Eigen::Ref.
Internally, it keeps an instance of Eigen::Ref<T> and provides access to it via operator* and operator->. As with ordinary pointers, these operators do not perform nullptr checks in Release builds. User-facing APIs should check for nullptr explicitly.
The primary motivation of this class is to follow GSG's "output arguments should be pointers" convention while taking advantage of using Eigen::Ref. It can also be used to pass optional Eigen objects since EigenPtr, unlike Eigen::Ref, can be null.
Some examples:
void foo(Eigen::Ref<Eigen::MatrixXd> M) {
M(0, 0) = 0;
}
foo(M);
foo(M.block(0, 0, 2, 2));
DRAKE_THROW_UNLESS(M != nullptr);
(*M)(0, 0) = 0;
}
foo(&M);
auto tmp = M.block(0, 0, 2, 2);
foo(&tmp);
This wrapper class provides a way to write non-template functions taking raw pointers to Eigen object...
Definition eigen_types.h:306
Notice that methods taking an EigenPtr can mutate the entries of a matrix as in method foo() in the example code above, but cannot change its size. This is because operator* and operator-> return an Eigen::Ref<T> object and only plain matrices/arrays can be resized and not expressions. This is the desired behavior, since resizing the block of a matrix or even a more general expression should not be allowed. If you do want to be able to resize a mutable matrix argument, then you must pass it as a Matrix<T>*, like so:
void bar(Eigen::MatrixXd* M) {
DRAKE_THROW_UNLESS(M != nullptr);
if (M->rows() != 4 && M->cols() != 3) {
M->resize(4, 3);
}
(*M)(0, 0) = 0;
}
- Note
- This class provides a way to avoid the const_cast hack introduced in Eigen's documentation.