Drake
body.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 
8 #include "drake/common/unused.h"
14 
15 namespace drake {
16 namespace multibody {
17 
18 // Forward declaration for BodyFrame<T>.
19 template<typename T> class Body;
20 
56 template <typename T>
57 class BodyFrame final : public Frame<T> {
58  public:
60 
62  const systems::Context<T>&) const override {
63  return Isometry3<T>::Identity();
64  }
65 
67  const systems::Context<T>&,
68  const Isometry3<T>& X_FQ) const override {
69  return X_FQ;
70  }
71 
72  protected:
73  // Frame<T>::DoCloneToScalar() overrides.
74  std::unique_ptr<Frame<double>> DoCloneToScalar(
75  const MultibodyTree<double>& tree_clone) const override;
76 
77  std::unique_ptr<Frame<AutoDiffXd>> DoCloneToScalar(
78  const MultibodyTree<AutoDiffXd>& tree_clone) const override;
79 
80  private:
81  // Body<T> and BodyFrame<T> are natural allies. A BodyFrame object is created
82  // every time a Body object is created and they are associated with each
83  // other.
84  friend class Body<T>;
85 
86  // Make BodyFrame templated on any other scalar type a friend of
87  // BodyFrame<T> so that CloneToScalar<ToAnyOtherScalar>() can access
88  // private methods from BodyFrame<T>.
89  template <typename> friend class BodyFrame;
90 
91  // Only Body objects can create BodyFrame objects since Body is a friend of
92  // BodyFrame.
93  explicit BodyFrame(const Body<T>& body) : Frame<T>(body) {}
94 
95  // Helper method to make a clone templated on any other scalar type.
96  // This method holds the common implementation for the different overrides to
97  // DoCloneToScalar().
98  template <typename ToScalar>
99  std::unique_ptr<Frame<ToScalar>> TemplatedDoCloneToScalar(
100  const MultibodyTree<ToScalar>& tree_clone) const;
101 };
102 
103 // Forward declarations for Body<T>.
104 template<typename T> class MultibodyTree;
105 
107 // Internal implementation details. Users should not access implementations
108 // in this namespace.
109 namespace internal {
110 template <typename T>
111 // Attorney-Client idiom to grant MultibodyTree access to a selected set of
112 // private methods in Body.
113 // BodyAttorney serves as a "proxy" to the Body class but only providing an
114 // interface to a selected subset of methods that should be accessible to
115 // MultibodyTree. These methods are related to the construction and finalize
116 // stage of the multibody system.
117 class BodyAttorney {
118  private:
119  // MultibodyTree keeps a list of mutable pointers to each of the body frames
120  // in the system and therefore it needs mutable access.
121  // Notice this method is private and therefore users do not have access to it
122  // even in the rare event they'd attempt to peek into the "internal::"
123  // namespace.
124  static BodyFrame<T>& get_mutable_body_frame(Body<T>* body) {
125  return body->get_mutable_body_frame();
126  }
127  friend class MultibodyTree<T>;
128 };
129 } // namespace internal
131 
144 template <typename T>
145 class Body : public MultibodyTreeElement<Body<T>, BodyIndex> {
146  public:
148 
149 
150  Body() : body_frame_(*this) {}
151 
154  virtual int get_num_flexible_positions() const = 0;
155 
158  virtual int get_num_flexible_velocities() const = 0;
159 
161  const BodyFrame<T>& get_body_frame() const {
162  return body_frame_;
163  }
164 
168  return topology_.body_node;
169  }
170 
172  virtual T get_mass(const MultibodyTreeContext<T> &context) const = 0;
173 
177  virtual const Vector3<T> CalcCenterOfMassInBodyFrame(
178  const MultibodyTreeContext<T>& context) const = 0;
179 
188  virtual SpatialInertia<T> CalcSpatialInertiaInBodyFrame(
189  const MultibodyTreeContext<T>& context) const = 0;
190 
197  template <typename ToScalar>
198  std::unique_ptr<Body<ToScalar>> CloneToScalar(
199  const MultibodyTree<ToScalar>& tree_clone) const {
200  return DoCloneToScalar(tree_clone);
201  }
202 
203  protected:
218 
220  virtual std::unique_ptr<Body<double>> DoCloneToScalar(
221  const MultibodyTree<double>& tree_clone) const = 0;
222 
224  virtual std::unique_ptr<Body<AutoDiffXd>> DoCloneToScalar(
225  const MultibodyTree<AutoDiffXd>& tree_clone) const = 0;
226 
228 
229  private:
230  // Only friends of BodyAttorney (i.e. MultibodyTree) have access to a selected
231  // set of private Body methods.
232  friend class internal::BodyAttorney<T>;
233 
234  // Implementation for MultibodyTreeElement::DoSetTopology().
235  // At MultibodyTree::Finalize() time, each body retrieves its topology
236  // from the parent MultibodyTree.
237  void DoSetTopology(const MultibodyTreeTopology& tree_topology) final {
238  topology_ = tree_topology.get_body(this->get_index());
239  body_frame_.SetTopology(tree_topology);
240  }
241 
242  // MultibodyTree has access to the mutable BodyFrame through BodyAttorney.
243  BodyFrame<T>& get_mutable_body_frame() {
244  return body_frame_;
245  }
246 
247  // Body frame associated with this body.
248  BodyFrame<T> body_frame_;
249 
250  // The internal bookkeeping topology struct used by MultibodyTree.
251  BodyTopology topology_;
252 };
253 
254 } // namespace multibody
255 } // namespace drake
Eigen::Transform< Scalar, 3, Eigen::Isometry > Isometry3
An Isometry templated on scalar type.
Definition: eigen_types.h:106
MultibodyTreeContext is an object that contains all the information needed to uniquely determine the ...
Definition: multibody_tree_context.h:40
Definition: automotive_demo.cc:88
Data structure to store the topological information associated with a Body.
Definition: multibody_tree_topology.h:38
std::unique_ptr< Frame< double > > DoCloneToScalar(const MultibodyTree< double > &tree_clone) const override
Clones this Frame (templated on T) to a frame templated on double.
Definition: body.cc:24
Context is an abstract base class template that represents all the inputs to a System: time...
Definition: query_handle.h:10
Body provides the general abstraction of a body with an API that makes no assumption about whether a ...
Definition: body.h:19
BodyNodeIndex get_node_index() const
Returns the index of the node in the underlying tree structure of the parent MultibodyTree to which t...
Definition: body.h:167
const BodyFrame< T > & get_body_frame() const
Returns a const reference to the associated BodyFrame.
Definition: body.h:161
A BodyFrame is a material Frame that serves as the unique reference frame for a Body.
Definition: body.h:57
Frame is an abstract class representing a material frame (also called a physical frame), meaning that it is associated with a material point of a Body.
Definition: frame.h:41
Eigen::Matrix< Scalar, 3, 1 > Vector3
A column vector of size 3, templated on scalar type.
Definition: eigen_types.h:34
Data structure to store the topological information associated with an entire MultibodyTree.
Definition: multibody_tree_topology.h:372
This file defines the topological structures which represent the logical connectivities between multi...
friend class BodyFrame
Definition: body.h:89
Isometry3< T > CalcOffsetPoseInBody(const systems::Context< T > &, const Isometry3< T > &X_FQ) const override
Given the offset pose X_FQ of a frame Q in this frame F, this method computes the pose X_BQ of frame ...
Definition: body.h:66
std::unique_ptr< Body< ToScalar > > CloneToScalar(const MultibodyTree< ToScalar > &tree_clone) const
NVI (Non-Virtual Interface) to DoCloneToScalar() templated on the scalar type of the new clone to be ...
Definition: body.h:198
This class represents the physical concept of a Spatial Inertia.
Definition: spatial_inertia.h:87
#define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname)
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:33
Isometry3< T > CalcPoseInBodyFrame(const systems::Context< T > &) const override
Returns the pose X_BF of this frame F in the body frame B associated with this frame.
Definition: body.h:61
Provides careful macros to selectively enable or disable the special member functions for copy-constr...