For instance, if I do auto my_qs = MBP::GetMutablePositions(context) to retrieve a reference to the positions vector, everything depending on q's will get invalidated. Say I compute a pose and therefore position kinematics gets validated. If I later do my_qs.setZero(), nothing depending on qs will get invalidated leading to very obscure and difficult to track faulty computations.
The proposed solution is to deprecate the GetMutableFoo() methods in favor of SetFoo() methods.
@edrumwri, you worked on these in the past, what do you think?
The method already has pretty scary doxygen. Did you happen to step on a landmine anyway, and that's why this is coming up now?
True, thanks for pointing that out @jwnimmer-tri. Yes, I did step on the landmine of finding this method before its sibling (precisely and oddly when adding more MBP cache entries).
Why do we even have this method in there if its so dangerous then? why not just only provide SetFoo()?
Well, at least right now its useful because Set might forces an allocation (because it's argument is a const Eigen::Vector& not an Eigen::Ref). Even if that's repaired, I could imagine Mutable still being useful if the user wants to invalidate the entire position state at once, and then populate it model-by-model or body-by-body piecewise (possibly for just a few bodies) -- so you're not copying the whole q every time you just want to change a single pose.
It's possible that in addition to the scary doxygen, we should make the method names even scarier also. But this question applies to _all_ of the GetMutable methods such as the Context, State, etc. I thought that @sherm1 already had an issue about that, but I'm not immediately seeing it. We should probably take a consistent solution throughout.
Oh, actually I thought you were talking about the similar methods on Context. Now I see these are MultibodyPlant methods and I do wonder why we should have them at all.
Most helpful comment
The method already has pretty scary doxygen. Did you happen to step on a landmine anyway, and that's why this is coming up now?