Choreonoid  1.8
BodyItem.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_PLUGIN_BODY_ITEM_H
7 #define CNOID_BODY_PLUGIN_BODY_ITEM_H
8 
9 #include <cnoid/Item>
10 #include <cnoid/Body>
11 #include <cnoid/CollisionLinkPair>
12 #include <cnoid/LocatableItem>
13 #include <cnoid/RenderableItem>
14 #include <cnoid/stdx/optional>
15 #include "exportdecl.h"
16 
17 namespace cnoid {
18 
19 class ItemManager;
20 class BodyState;
21 class LinkKinematicsKit;
22 class InverseKinematics;
23 class PinDragIK;
24 class PenetrationBlocker;
25 class EditableSceneBody;
26 class ItemFileIO;
27 
28 class CNOID_EXPORT BodyItem : public Item, public LocatableItem, public RenderableItem
29 {
30 public:
31  static void initializeClass(ExtensionManager* ext);
32 
33  // The following functions are Implemented in BodyItemFileIO.cpp
34  static void registerBodyItemFileIoSet(ItemManager* im);
36  static ItemFileIO* bodyFileIO();
37  static ItemFileIO* meshFileIO();
38 
39  BodyItem();
40  BodyItem(const std::string& name);
41  BodyItem(const BodyItem& org);
42  virtual ~BodyItem();
43 
44  virtual bool setName(const std::string& name) override;
45 
46  Body* body() const;
47  void setBody(Body* body);
48 
49  bool isSharingShapes() const;
50  void cloneShapes(CloneMap& cloneMap);
51 
52  bool makeBodyStatic();
53  bool makeBodyDynamic();
54 
55  // API for a composite body
56  // The following body and link pair is basically determined by
57  // the parent-child relationship in the item tree
58  BodyItem* parentBodyItem();
59  // True if the body is attached to the parent body with a holder device and an attachment device
60  bool isAttachedToParentBody() const { return isAttachedToParentBody_; }
61  void setAttachmentEnabled(bool on, bool doNotifyUpdate = true);
62  bool isAttachmentEnabled() const;
63  bool attachToParentBody(bool doNotifyUpdate = true);
64 
65  // The current parent body can temporarily be changed by this function
66  //void setTemporalParentBodyItem(BodyItem* parentBodyItem);
67  // The parent body item defined by the parent-child relationship in the item tree is restored
68  // if the relationship exists. Otherwise, the parent body item is cleared.
69  //void resetParentBodyItem();
70 
71  void moveToOrigin();
72  enum PresetPoseID { INITIAL_POSE, STANDARD_POSE };
73  void setPresetPose(PresetPoseID id);
74 
75  Link* currentBaseLink() const;
76  void setCurrentBaseLink(Link* link);
77 
78  void calcForwardKinematics(bool calcVelocity = false, bool calcAcceleration = false);
79 
80  void copyKinematicState();
81  void pasteKinematicState();
82 
83  void storeKinematicState(BodyState& state);
84  bool restoreKinematicState(const BodyState& state);
85 
86  void storeInitialState();
87  void restoreInitialState(bool doNotify = true);
88 
89  void getInitialState(BodyState& out_state);
90  void setInitialState(const BodyState& in_state);
91 
92  [[deprecated("This function does nothing.")]]
93  void beginKinematicStateEdit();
94  [[deprecated("This function does nothing.")]]
95  void cancelKinematicStateEdit();
96  [[deprecated("Use notifyKinematicStateEdited")]]
97  void acceptKinematicStateEdit();
98 
99  LinkKinematicsKit* findPresetLinkKinematicsKit(Link* targetLink = nullptr);
100  std::shared_ptr<InverseKinematics> findPresetIK(Link* targetLink);
101  LinkKinematicsKit* getCurrentLinkKinematicsKit(Link* targetLink);
102  std::shared_ptr<InverseKinematics> getCurrentIK(Link* targetLink);
103  std::shared_ptr<PinDragIK> getOrCreatePinDragIK();
104  std::shared_ptr<PinDragIK> checkPinDragIK();
105  std::shared_ptr<PenetrationBlocker> createPenetrationBlocker(Link* link, bool excludeSelfCollisions = false);
106 
107  [[deprecated("Create a new BodyItem and use Item::replace to update the model.")]]
108  SignalProxy<void()> sigModelUpdated();
109  [[deprecated("Create a new BodyItem and use Item::replace to update the model.")]]
110  void notifyModelUpdate();
111 
117  SignalProxy<void()> sigKinematicStateChanged();
118 
119  void notifyKinematicStateChange(
120  bool requestFK = false, bool requestVelFK = false, bool requestAccFK = false);
121  void notifyKinematicStateChange(
122  Connection& connectionToBlock,
123  bool requestFK = false, bool requestVelFK = false, bool requestAccFK = false);
124  void notifyKinematicStateChangeLater(
125  bool requestFK = false, bool requestVelFK = false, bool requestAccFK = false);
126  void notifyKinematicStateChangeLater(
127  Connection& connectionToBlock,
128  bool requestFK = false, bool requestVelFK = false, bool requestAccFK = false);
129 
135  SignalProxy<void()> sigKinematicStateUpdated();
136 
137  void notifyKinematicStateUpdate(bool doNotifyStateChange = true);
138 
139  bool isCollisionDetectionEnabled() const;
140  void setCollisionDetectionEnabled(bool on);
141  [[deprecated("Use setCollisionDetectionEnabled")]]
142  void enableCollisionDetection(bool on) { setCollisionDetectionEnabled(on); }
143 
144  bool isSelfCollisionDetectionEnabled() const;
145  void setSelfCollisionDetectionEnabled(bool on);
146  [[deprecated("Use setSelfCollisionDetectionEnabled")]]
147  void enableSelfCollisionDetection(bool on) { setSelfCollisionDetectionEnabled(on); }
148 
149  void clearCollisions();
150 
151  std::vector<CollisionLinkPairPtr>& collisions() { return collisions_; }
152  const std::vector<CollisionLinkPairPtr>& collisions() const { return collisions_; }
153  std::vector<bool>& collisionLinkBitSet() { return collisionLinkBitSet_; }
154  const std::vector<bool>& collisionLinkBitSet() const { return collisionLinkBitSet_; }
155  std::vector<CollisionLinkPairPtr>& collisionsOfLink(int linkIndex) { return collisionsOfLink_[linkIndex]; }
156  const std::vector<CollisionLinkPairPtr>& collisionsOfLink(int linkIndex) const { return collisionsOfLink_[linkIndex]; }
157  SignalProxy<void()> sigCollisionsUpdated() { return sigCollisionsUpdated_; }
158  void notifyCollisionUpdate() { sigCollisionsUpdated_(); }
159 
160  const Vector3& centerOfMass();
161 
162  bool isLeggedBody() const;
163  bool doLegIkToMoveCm(const Vector3& c, bool onlyProjectionToFloor = false);
164 
165  const Vector3& zmp() const;
166  void setZmp(const Vector3& zmp);
167  void editZmp(const Vector3& zmp);
168 
169  enum PositionType { CM_PROJECTION, HOME_COP, RIGHT_HOME_COP, LEFT_HOME_COP, ZERO_MOMENT_POINT };
170 
171  stdx::optional<Vector3> getParticularPosition(PositionType posType);
172 
173  bool setStance(double width);
174 
175  // LocatableItem function
176  virtual LocationProxyPtr getLocationProxy() override;
177 
178  bool isLocationEditable() const;
179  void setLocationEditable(bool on);
180  LocationProxyPtr createLinkLocationProxy(Link* link);
181 
182  // RenderableItem function
183  virtual SgNode* getScene() override;
184 
185  EditableSceneBody* sceneBody();
186  EditableSceneBody* existingSceneBody();
187  float transparency() const;
188  void setTransparency(float t);
189 
190  class Impl;
191 
192 protected:
193  virtual Item* doDuplicate() const override;
194  virtual void doAssign(Item* item) override;
195  virtual void onTreePathChanged() override;
196  virtual void onConnectedToRoot() override;
197  virtual void doPutProperties(PutPropertyFunction& putProperty) override;
198  virtual bool store(Archive& archive) override;
199  virtual bool restore(const Archive& archive) override;
200 
201 private:
202  Impl* impl;
203  bool isAttachedToParentBody_;
204  std::vector<CollisionLinkPairPtr> collisions_;
205  std::vector<bool> collisionLinkBitSet_;
206  std::vector<std::vector<CollisionLinkPairPtr>> collisionsOfLink_;
207  Signal<void()> sigCollisionsUpdated_;
208 
209  friend class PyBodyPlugin;
210 };
211 
213 
214 }
215 
216 #endif
cnoid::ItemFileIO
Definition: ItemFileIO.h:19
cnoid::BodyItem::PositionType
PositionType
Definition: BodyItem.h:169
cnoid::BodyItem::collisionsOfLink
std::vector< CollisionLinkPairPtr > & collisionsOfLink(int linkIndex)
Definition: BodyItem.h:155
cnoid::Vector3
Eigen::Vector3d Vector3
Definition: EigenTypes.h:57
cnoid::ExtensionManager
Definition: ExtensionManager.h:23
cnoid::BodyItem::collisionLinkBitSet
const std::vector< bool > & collisionLinkBitSet() const
Definition: BodyItem.h:154
cnoid::BodyState
Definition: BodyState.h:20
cnoid::BodyItem::collisions
const std::vector< CollisionLinkPairPtr > & collisions() const
Definition: BodyItem.h:152
cnoid::PutPropertyFunction
Definition: PutPropertyFunction.h:51
cnoid::EditableSceneBody
Definition: EditableSceneBody.h:45
cnoid::Archive
Definition: Archive.h:22
cnoid::BodyItem::enableSelfCollisionDetection
void enableSelfCollisionDetection(bool on)
Definition: BodyItem.h:147
cnoid::ref_ptr< LocationProxy >
cnoid::BodyItem
Definition: BodyItem.h:28
cnoid::BodyItem::isAttachedToParentBody
bool isAttachedToParentBody() const
Definition: BodyItem.h:60
cnoid::BodyItem::enableCollisionDetection
void enableCollisionDetection(bool on)
Definition: BodyItem.h:142
cnoid::BodyItem::collisionsOfLink
const std::vector< CollisionLinkPairPtr > & collisionsOfLink(int linkIndex) const
Definition: BodyItem.h:156
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::Item
Definition: Item.h:29
cnoid::RenderableItem
Definition: RenderableItem.h:11
cnoid::LinkKinematicsKit
Definition: LinkKinematicsKit.h:23
cnoid::CloneMap
Definition: CloneMap.h:13
cnoid::BodyItem::PresetPoseID
PresetPoseID
Definition: BodyItem.h:72
cnoid::BodyItem::collisionLinkBitSet
std::vector< bool > & collisionLinkBitSet()
Definition: BodyItem.h:153
cnoid::BodyItemPtr
ref_ptr< BodyItem > BodyItemPtr
Definition: BodyItem.h:212
cnoid::BodyItem::sigCollisionsUpdated
SignalProxy< void()> sigCollisionsUpdated()
Definition: BodyItem.h:157
cnoid::Body
Definition: Body.h:28
cnoid::Signal
Definition: Signal.h:165
cnoid::BodyItem::collisions
std::vector< CollisionLinkPairPtr > & collisions()
Definition: BodyItem.h:151
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::ItemManager
Definition: ItemManager.h:59
cnoid::Connection
Definition: Signal.h:206
cnoid::SgNode
Definition: SceneGraph.h:157
cnoid::LocatableItem
Definition: LocatableItem.h:62
cnoid::BodyItem::notifyCollisionUpdate
void notifyCollisionUpdate()
Definition: BodyItem.h:158