Choreonoid  1.8
Body.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_BODY_H
7 #define CNOID_BODY_BODY_H
8 
9 #include "LinkTraverse.h"
10 #include "ExtraJoint.h"
11 #include "DeviceList.h"
12 #include "exportdecl.h"
13 
14 namespace cnoid {
15 
16 class Body;
17 class BodyHandler;
18 class Link;
19 class Mapping;
20 class CloneMap;
21 
22 struct BodyInterface;
24 typedef void* BodyCustomizerHandle;
25 
27 
28 class CNOID_EXPORT Body : public ClonableReferenced
29 {
30 public:
31  Body();
32  Body(const std::string& name);
33  Body(const Body& org) = delete;
34  virtual ~Body();
35 
36  // This function can only be used just after the construction of a new instance
37  void copyFrom(const Body* org, CloneMap* cloneMap = nullptr);
38  Body* clone() const { return static_cast<Body*>(doClone(nullptr)); }
39  Body* clone(CloneMap& cloneMap) const { return static_cast<Body*>(doClone(&cloneMap)); }
40 
41  virtual Link* createLink(const Link* org = nullptr) const;
42 
43  const std::string& name() const;
44  void setName(const std::string& name);
45  const std::string& modelName() const;
46  void setModelName(const std::string& name);
47 
48  void setRootLink(Link* link);
49 
50  [[deprecated("This func. does nothing")]]
52 
56  void updateLinkTree();
57 
58  void initializePosition();
59  virtual void initializeState();
60 
61  Body* parentBody() const { return parentBodyLink_ ? parentBodyLink_->body() : nullptr; }
62  Link* parentBodyLink() const { return parentBodyLink_; }
63  void setParent(Link* parentBodyLink);
64  void resetParent();
65  void syncPositionWithParentBody(bool doForwardKinematics = true);
66 
71  int numLinks() const {
72  return linkTraverse_.numLinks();
73  }
74 
80  Link* link(int index) const {
81  return linkTraverse_.link(index);
82  }
83 
88  Link* link(const std::string& name) const;
89 
93  const LinkTraverse& linkTraverse() const {
94  return linkTraverse_;
95  }
96 
97  const std::vector<Link*>& links() const {
98  return linkTraverse_.links();
99  }
100 
104  Link* rootLink() const {
105  return rootLink_;
106  }
107 
108  Link* findUniqueEndLink() const;
109  Link* lastSerialLink() const;
110 
120  int numJoints() const {
121  return numActualJoints;
122  }
123 
129  int numVirtualJoints() const {
130  return static_cast<int>(jointIdToLinkArray.size()) - numActualJoints;
131  }
132 
136  int numAllJoints() const {
137  return static_cast<int>(jointIdToLinkArray.size());
138  }
139 
145  Link* joint(int id) const {
146  return jointIdToLinkArray[id];
147  }
148 
153  Link* joint(const std::string& name) const;
154 
155  template<class Container> class ContainerWrapper {
156  public:
157  typedef typename Container::iterator iterator;
158  ContainerWrapper(iterator begin, iterator end) : begin_(begin), end_(end) { }
159  iterator begin() { return begin_; }
160  iterator end() { return end_; }
161  private:
162  iterator begin_;
163  iterator end_;
164  };
165 
168  jointIdToLinkArray.begin(), jointIdToLinkArray.begin() + numActualJoints);
169  }
170 
171  const std::vector<LinkPtr>& allJoints() const { return jointIdToLinkArray; }
172 
173  int numDevices() const {
174  return static_cast<int>(devices_.size());
175  }
176 
177  Device* device(int index) const { return devices_[index]; }
178 
185  const DeviceList<>& devices() const {
186  return devices_;
187  }
188 
189  template<class DeviceType> DeviceList<DeviceType> devices() const {
190  return devices_;
191  }
192 
193  template<class DeviceType> DeviceType* findDevice(const std::string& name) const {
194  return dynamic_cast<DeviceType*>(findDeviceSub(name));
195  }
196 
197  template<class DeviceType> DeviceType* findDevice() const {
198  for(auto& device : devices_)
199  if(auto found = dynamic_cast<DeviceType*>(device.get()))
200  return found;
201  return nullptr;
202  }
203 
204  Device* findDevice(const std::string& name) const {
205  return findDeviceSub(name);
206  }
207 
208  void addDevice(Device* device, Link* link);
209  [[deprecated("Use addDevice(Device* device, Link* link)")]]
210  void addDevice(Device* device);
211  void removeDevice(Device* device);
212  void clearDevices();
213  void sortDevicesByLinkOrder();
214  void initializeDeviceStates();
215 
219  bool isStaticModel() const {
220  return isStaticModel_;
221  }
222  bool isFixedRootModel() const {
223  return rootLink_->isFixedJoint();
224  }
225 
226  void resetDefaultPosition(const Isometry3& T);
227  const Isometry3& defaultPosition() const { return rootLink_->Tb(); }
228 
229  double mass() const;
230 
231  const Vector3& calcCenterOfMass();
232  const Vector3& centerOfMass() const;
233 
234  void calcTotalMomentum(Vector3& out_P, Vector3& out_L);
235 
236  void calcForwardKinematics(bool calcVelocity = false, bool calcAcceleration = false) {
237  linkTraverse_.calcForwardKinematics(calcVelocity, calcAcceleration);
238  }
239 
240  void clearExternalForces();
241 
242  int numExtraJoints() const { return static_cast<int>(extraJoints_.size()); }
243  ExtraJoint& extraJoint(int index) { return extraJoints_[index]; }
244  const ExtraJoint& extraJoint(int index) const { return extraJoints_[index]; }
245  void addExtraJoint(const ExtraJoint& extraJoint) { extraJoints_.push_back(extraJoint); }
246  void clearExtraJoints() { extraJoints_.clear(); }
247 
248  const Mapping* info() const;
249  Mapping* info();
250 
251  template<typename T> T info(const std::string& key) const;
252  template<typename T> T info(const std::string& key, const T& defaultValue) const;
253  template<typename T> void setInfo(const std::string& key, const T& value);
254 
255  void resetInfo(Mapping* info);
256 
257  void cloneShapes(CloneMap& cloneMap);
258 
259  template<class T> T* findCache(const std::string& name){
260  return dynamic_cast<T*>(findCacheSub(name));
261  }
262 
263  template<class T> const T* findCache(const std::string& name) const {
264  return dynamic_cast<const T*>(findCacheSub(name));
265  }
266 
267  template<class T> T* getOrCreateCache(const std::string& name){
268  T* cache = findCache<T>(name);
269  if(!cache){
270  cache = new T();
271  insertCache(name, cache);
272  }
273  return cache;
274  }
275 
276  void setCache(const std::string& name, Referenced* cache){
277  insertCache(name, cache);
278  }
279 
280  bool getCaches(PolymorphicReferencedArrayBase<>& out_caches, std::vector<std::string>& out_names) const;
281 
282  void removeCache(const std::string& name);
283 
284  void setCurrentTimeFunction(std::function<double()> func);
285  double currentTime() const { return currentTimeFunction(); }
286 
287  bool addHandler(BodyHandler* handler, bool isTopPriority = false);
288 
289  template<class BodyHandlerType> BodyHandlerType* findHandler(){
290  return dynamic_cast<BodyHandlerType*>(
291  findHandler([](BodyHandler* handler)->bool{ return dynamic_cast<BodyHandlerType*>(handler); }));
292  }
293 
294  int numHandlers() const;
295  BodyHandler* handler(int index);
296 
297  // The following functions for the body customizer are deprecated
298  BodyCustomizerHandle customizerHandle() const;
299  BodyCustomizerInterface* customizerInterface() const;
300  bool installCustomizer();
301  bool installCustomizer(BodyCustomizerInterface* customizerInterface);
302  bool hasVirtualJointForces() const;
303  void setVirtualJointForces(double timeStep = 0.0);
304  static void addCustomizerDirectory(const std::string& path);
305  static BodyInterface* bodyInterface();
306 
307  void resetLinkName(Link* link, const std::string& name);
308  void resetJointSpecificName(Link* link);
309  void resetJointSpecificName(Link* link, const std::string& name);
310 
311 protected:
312  Body(Link* rootLink);
313  virtual Referenced* doClone(CloneMap* cloneMap) const override;
314 
315 private:
316  LinkTraverse linkTraverse_;
317  LinkPtr rootLink_;
318  LinkPtr parentBodyLink_;
319  bool isStaticModel_;
320  std::vector<LinkPtr> jointIdToLinkArray;
321  int numActualJoints;
322  DeviceList<> devices_;
323  std::vector<ExtraJoint> extraJoints_;
324  std::function<double()> currentTimeFunction;
325 
326  class Impl;
327  Impl* impl;
328 
329  Link* cloneLinkTree(const Link* orgLink, CloneMap* cloneMap);
330  Link* createEmptyJoint(int jointId);
331  Device* findDeviceSub(const std::string& name) const;
332  Referenced* findCacheSub(const std::string& name);
333  const Referenced* findCacheSub(const std::string& name) const;
334  void insertCache(const std::string& name, Referenced* cache);
335  BodyHandler* findHandler(std::function<bool(BodyHandler*)> isTargetHandlerType);
336 };
337 
338 template<> CNOID_EXPORT double Body::info(const std::string& key) const;
339 template<> CNOID_EXPORT double Body::info(const std::string& key, const double& defaultValue) const;
340 template<> CNOID_EXPORT bool Body::info(const std::string& key, const bool& defaultValue) const;
341 template<> CNOID_EXPORT void Body::setInfo(const std::string& key, const double& value);
342 template<> CNOID_EXPORT void Body::setInfo(const std::string& key, const bool& value);
343 
344 }
345 
346 #endif
cnoid::BodyPtr
ref_ptr< Body > BodyPtr
Definition: Body.h:26
cnoid::Body::getOrCreateCache
T * getOrCreateCache(const std::string &name)
Definition: Body.h:267
cnoid::Body::numLinks
int numLinks() const
Definition: Body.h:71
cnoid::Body::parentBody
Body * parentBody() const
Definition: Body.h:61
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::Body::extraJoint
const ExtraJoint & extraJoint(int index) const
Definition: Body.h:244
cnoid::Body::setInfo
void setInfo(const std::string &key, const T &value)
cnoid::Body::clone
Body * clone() const
Definition: Body.h:38
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::Body::parentBodyLink
Link * parentBodyLink() const
Definition: Body.h:62
cnoid::Body::device
Device * device(int index) const
Definition: Body.h:177
cnoid::Body::findDevice
DeviceType * findDevice(const std::string &name) const
Definition: Body.h:193
cnoid::Body::setCache
void setCache(const std::string &name, Referenced *cache)
Definition: Body.h:276
ExtraJoint.h
cnoid::Body::ContainerWrapper::iterator
Container::iterator iterator
Definition: Body.h:157
cnoid::Body::clearExtraJoints
void clearExtraJoints()
Definition: Body.h:246
cnoid::Isometry3
Eigen::Isometry3d Isometry3
Definition: EigenTypes.h:73
cnoid::Body::findHandler
BodyHandlerType * findHandler()
Definition: Body.h:289
cnoid::Body::findCache
const T * findCache(const std::string &name) const
Definition: Body.h:263
cnoid::Body::defaultPosition
const Isometry3 & defaultPosition() const
Definition: Body.h:227
cnoid::Body::numAllJoints
int numAllJoints() const
Definition: Body.h:136
cnoid::Body::numJoints
int numJoints() const
Definition: Body.h:120
cnoid::Body::clone
Body * clone(CloneMap &cloneMap) const
Definition: Body.h:39
cnoid::Body::findDevice
Device * findDevice(const std::string &name) const
Definition: Body.h:204
cnoid::Body::expandLinkOffsetRotations
void expandLinkOffsetRotations()
Definition: Body.h:51
cnoid::ref_ptr< Body >
cnoid::Body::ContainerWrapper::begin
iterator begin()
Definition: Body.h:159
cnoid::Body::isFixedRootModel
bool isFixedRootModel() const
Definition: Body.h:222
cnoid::BodyCustomizerInterface
Definition: BodyCustomizerInterface.h:54
LinkTraverse.h
The header file of the LinkTraverse class.
cnoid::Body::rootLink
Link * rootLink() const
Definition: Body.h:104
cnoid::BodyInterface
Definition: BodyCustomizerInterface.h:26
cnoid
Definition: AbstractSceneLoader.h:11
DeviceList.h
cnoid::ClonableReferenced
Definition: ClonableReferenced.h:10
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::Body::devices
DeviceList< DeviceType > devices() const
Definition: Body.h:189
cnoid::Body::numDevices
int numDevices() const
Definition: Body.h:173
cnoid::Body::joints
ContainerWrapper< std::vector< LinkPtr > > joints()
Definition: Body.h:166
cnoid::Body::ContainerWrapper::end
iterator end()
Definition: Body.h:160
cnoid::BodyHandler
Definition: BodyHandler.h:20
cnoid::LinkTraverse
Definition: LinkTraverse.h:17
cnoid::ExtraJoint
Definition: ExtraJoint.h:13
cnoid::Body::link
Link * link(int index) const
Definition: Body.h:80
cnoid::BodyCustomizerHandle
void * BodyCustomizerHandle
Definition: Body.h:23
cnoid::Referenced
Definition: Referenced.h:54
cnoid::Body::currentTime
double currentTime() const
Definition: Body.h:285
cnoid::PolymorphicReferencedArrayBase
Definition: PolymorphicReferencedArray.h:14
cnoid::Body::findCache
T * findCache(const std::string &name)
Definition: Body.h:259
cnoid::Body::devices
const DeviceList & devices() const
Definition: Body.h:185
cnoid::Body::addExtraJoint
void addExtraJoint(const ExtraJoint &extraJoint)
Definition: Body.h:245
cnoid::Body::findDevice
DeviceType * findDevice() const
Definition: Body.h:197
cnoid::Body
Definition: Body.h:28
cnoid::Body::numVirtualJoints
int numVirtualJoints() const
Definition: Body.h:129
cnoid::Device
Definition: Device.h:53
cnoid::Body::ContainerWrapper
Definition: Body.h:155
cnoid::Body::links
const std::vector< Link * > & links() const
Definition: Body.h:97
cnoid::Body::isStaticModel
bool isStaticModel() const
Definition: Body.h:219
cnoid::Body::joint
Link * joint(int id) const
Definition: Body.h:145
cnoid::Body::linkTraverse
const LinkTraverse & linkTraverse() const
Definition: Body.h:93
cnoid::DeviceList
Definition: DeviceList.h:16
cnoid::Body::allJoints
const std::vector< LinkPtr > & allJoints() const
Definition: Body.h:171
cnoid::Body::info
const Mapping * info() const
Definition: Body.cpp:388
cnoid::Body::calcForwardKinematics
void calcForwardKinematics(bool calcVelocity=false, bool calcAcceleration=false)
Definition: Body.h:236
cnoid::Body::extraJoint
ExtraJoint & extraJoint(int index)
Definition: Body.h:243
cnoid::Body::ContainerWrapper::ContainerWrapper
ContainerWrapper(iterator begin, iterator end)
Definition: Body.h:158
cnoid::Body::numExtraJoints
int numExtraJoints() const
Definition: Body.h:242