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));
(*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
#define DRAKE_THROW_UNLESS(condition,...)
Provides a convenient wrapper to throw an exception when a condition is unmet.
Definition drake_assert.h:104
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) {
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.
|
| | EigenPtr () |
| | EigenPtr (std::nullptr_t) |
| | Overload for nullptr.
|
| | EigenPtr (const EigenPtr &other) |
| | Copy constructor results in a reference to the given matrix type.
|
| template<typename PlainObjectTypeIn> |
| | EigenPtr (PlainObjectTypeIn *m) |
| | Constructs with a reference to another matrix type.
|
| template<typename PlainObjectTypeIn> |
| | EigenPtr (const EigenPtr< PlainObjectTypeIn > &other) |
| | Constructs from another EigenPtr.
|
| EigenPtr & | operator= (const EigenPtr &other) |
| | Copy assignment results in a reference to the given matrix type.
|
| template<typename PlainObjectTypeIn> |
| EigenPtr & | operator= (const EigenPtr< PlainObjectTypeIn > &other) |
| RefType & | operator* () const |
| RefType * | operator-> () const |
| | operator bool () const |
| | Returns whether or not this contains a valid reference.
|
| bool | operator== (std::nullptr_t) const |
| bool | operator!= (std::nullptr_t) const |