Choreonoid  1.8
BodyMotion.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_BODY_BODY_MOTION_H
7 #define CNOID_BODY_BODY_MOTION_H
8 
9 #include <cnoid/MultiValueSeq>
10 #include <cnoid/MultiSE3Seq>
11 #include <cnoid/Signal>
12 #include <cnoid/NullOut>
13 #include <map>
14 #include "exportdecl.h"
15 
16 namespace cnoid {
17 
18 class Body;
19 
20 class CNOID_EXPORT BodyMotion : public AbstractSeq
21 {
22 public:
23  BodyMotion();
24  BodyMotion(const BodyMotion& org);
25 
26  using AbstractSeq::operator=;
27  BodyMotion& operator=(const BodyMotion& rhs);
28  virtual std::shared_ptr<AbstractSeq> cloneSeq() const override;
29 
30  void setDimension(int numFrames, int numJoints, int numLinks, bool clearNewArea = false);
31  void setNumJoints(int numJoints, bool clearNewElements = false);
32 
33  int numLinks() const { return linkPosSeq_->numParts(); }
34  int numJoints() const { return jointPosSeq_->numParts(); }
35 
36  double frameRate() const;
37  virtual double getFrameRate() const override;
38  virtual void setFrameRate(double frameRate) override;
39 
40  double timeStep() const;
41 
42  virtual double getOffsetTime() const override;
43  virtual void setOffsetTime(double time) override;
44 
45  int numFrames() const;
46  virtual int getNumFrames() const override;
47  virtual void setNumFrames(int n, bool clearNewArea = false) override;
48 
49  std::shared_ptr<MultiSE3Seq> linkPosSeq() {
50  return linkPosSeq_;
51  }
52 
53  std::shared_ptr<const MultiSE3Seq> linkPosSeq() const {
54  return linkPosSeq_;
55  }
56 
57  std::shared_ptr<MultiValueSeq> jointPosSeq() {
58  return jointPosSeq_;
59  }
60 
61  std::shared_ptr<const MultiValueSeq> jointPosSeq() const {
62  return jointPosSeq_;
63  }
64 
65  class CNOID_EXPORT Frame {
66  BodyMotion& motion_;
67  int frame_;
68  Frame(BodyMotion& motion, int frame) : motion_(motion), frame_(frame) { }
69  Frame& operator=(const Frame&) { return *this; }
70  public:
71  Frame();
72  Frame(const Frame& org) : motion_(org.motion_), frame_(org.frame_) { }
73  ~Frame() { }
74  BodyMotion& motion() { return motion_; }
75  int frame() const { return frame_; }
76  friend class BodyMotion;
77  };
78 
79  class ConstFrame {
80  const BodyMotion& motion_;
81  int frame_;
82  ConstFrame(const BodyMotion& motion, int frame) : motion_(motion), frame_(frame) { }
83  ConstFrame& operator=(const ConstFrame&) { return *this; }
84  ConstFrame& operator=(const Frame&) { return *this; }
85  public:
86  ConstFrame();
87  ConstFrame(const ConstFrame& org) : motion_(org.motion_), frame_(org.frame_) { }
88  ConstFrame(const Frame& org) : motion_(org.motion_), frame_(org.frame_) { }
90  const BodyMotion& motion() const { return motion_; }
91  int frame() const { return frame_; }
92  friend class BodyMotion;
93  };
94 
95  Frame frame(int frame) { return Frame(*this, frame); }
96  ConstFrame frame(int frame) const { return ConstFrame(*this, frame); }
97 
98  bool load(const std::string& filename, std::ostream& os = nullout());
99  bool save(const std::string& filename, std::ostream& os = nullout());
100  bool save(const std::string& filename, double version, std::ostream& os = nullout());
101 
102  typedef std::map<std::string, std::shared_ptr<AbstractSeq>> ExtraSeqMap;
103  typedef ExtraSeqMap::const_iterator ConstSeqIterator;
104 
105  ConstSeqIterator extraSeqBegin() const { return extraSeqs.begin(); }
106  ConstSeqIterator extraSeqEnd() const { return extraSeqs.end(); }
107 
108  template <class SeqType>
109  std::shared_ptr<SeqType> extraSeq(const std::string& name) const {
110  ExtraSeqMap::const_iterator p = extraSeqs.find(name);
111  return ((p != extraSeqs.end()) ?
112  std::dynamic_pointer_cast<SeqType>(p->second) : std::shared_ptr<SeqType>());
113  }
114 
115  void setExtraSeq(const std::string& name, std::shared_ptr<AbstractSeq> seq);
116 
117  template <class SeqType>
118  std::shared_ptr<SeqType> getOrCreateExtraSeq(const std::string& name) {
119  std::shared_ptr<AbstractSeq>& base = extraSeqs[name];
120  std::shared_ptr<SeqType> seq;
121  if(base){
122  seq = std::dynamic_pointer_cast<SeqType>(base);
123  }
124  if(!seq){
125  seq = std::make_shared<SeqType>();
126  base = seq;
127  seq->setFrameRate(frameRate());
128  seq->setNumFrames(numFrames());
129  sigExtraSeqsChanged_();
130  }
131  return seq;
132  }
133 
134  void clearExtraSeq(const std::string& name);
135 
137  return sigExtraSeqsChanged_;
138  }
139 
141  void setNumParts(int numJoints, bool clearNewElements = false){
142  setNumJoints(numJoints, clearNewElements);
143  }
144 
146  int getNumParts() const { return numJoints(); }
147 
149  bool loadStandardYAMLformat(const std::string& filename, std::ostream& os = nullout()){
150  return load(filename, os);
151  }
153  bool saveAsStandardYAMLformat(const std::string& filename, std::ostream& os = nullout()){
154  return save(filename, os);
155  }
156 
157 protected:
158  virtual bool doReadSeq(const Mapping* archive, std::ostream& os) override;
159  virtual bool doWriteSeq(YAMLWriter& writer, std::function<void()> additionalPartCallback) override;
160 
161 private:
162  std::shared_ptr<MultiSE3Seq> linkPosSeq_;
163  std::shared_ptr<MultiValueSeq> jointPosSeq_;
164  ExtraSeqMap extraSeqs;
165  Signal<void()> sigExtraSeqsChanged_;
166 };
167 
168 CNOID_EXPORT BodyMotion::Frame operator<<(BodyMotion::Frame frame, const Body& body);
169 CNOID_EXPORT BodyMotion::Frame operator>>(BodyMotion::Frame frame, const Body& body);
170 CNOID_EXPORT BodyMotion::ConstFrame operator>>(BodyMotion::ConstFrame frame, Body& body);
171 CNOID_EXPORT Body& operator<<(Body& body, BodyMotion::Frame frame);
172 CNOID_EXPORT Body& operator<<(Body& body, BodyMotion::ConstFrame frame);
173 CNOID_EXPORT const Body& operator>>(const Body& body, BodyMotion::Frame frame);
174 
175 #ifdef CNOID_BACKWARD_COMPATIBILITY
176 typedef std::shared_ptr<BodyMotion> BodyMotionPtr;
177 #endif
178 
179 }
180 
181 #endif
cnoid::BodyMotion::extraSeqBegin
ConstSeqIterator extraSeqBegin() const
Definition: BodyMotion.h:105
cnoid::BodyMotion::sigExtraSeqsChanged
SignalProxy< void()> sigExtraSeqsChanged()
Definition: BodyMotion.h:136
cnoid::BodyMotion::ConstFrame::ConstFrame
ConstFrame(const Frame &org)
Definition: BodyMotion.h:88
cnoid::BodyMotion::Frame::Frame
Frame(const Frame &org)
Definition: BodyMotion.h:72
cnoid::BodyMotion::frame
Frame frame(int frame)
Definition: BodyMotion.h:95
cnoid::Mapping
Definition: ValueTree.h:253
cnoid::BodyMotion::loadStandardYAMLformat
bool loadStandardYAMLformat(const std::string &filename, std::ostream &os=nullout())
Definition: BodyMotion.h:149
cnoid::YAMLWriter
Definition: YAMLWriter.h:16
cnoid::BodyMotion::Frame::motion
BodyMotion & motion()
Definition: BodyMotion.h:74
cnoid::BodyMotion
Definition: BodyMotion.h:20
cnoid::BodyMotion::saveAsStandardYAMLformat
bool saveAsStandardYAMLformat(const std::string &filename, std::ostream &os=nullout())
Definition: BodyMotion.h:153
cnoid::BodyMotion::Frame
Definition: BodyMotion.h:65
cnoid::BodyMotion::ConstFrame
Definition: BodyMotion.h:79
cnoid::operator<<
CNOID_EXPORT std::ostream & operator<<(std::ostream &os, const BoundingBox &bb)
Definition: BoundingBox.cpp:160
cnoid::nullout
std::ostream & nullout()
Definition: NullOut.cpp:26
cnoid::BodyMotion::numJoints
int numJoints() const
Definition: BodyMotion.h:34
cnoid::BodyMotion::Frame::frame
int frame() const
Definition: BodyMotion.h:75
cnoid::BodyMotion::ConstFrame::~ConstFrame
~ConstFrame()
Definition: BodyMotion.h:89
cnoid::BodyMotion::frame
ConstFrame frame(int frame) const
Definition: BodyMotion.h:96
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::BodyMotion::getOrCreateExtraSeq
std::shared_ptr< SeqType > getOrCreateExtraSeq(const std::string &name)
Definition: BodyMotion.h:118
cnoid::BodyMotion::ConstFrame::motion
const BodyMotion & motion() const
Definition: BodyMotion.h:90
cnoid::BodyMotion::ConstSeqIterator
ExtraSeqMap::const_iterator ConstSeqIterator
Definition: BodyMotion.h:103
cnoid::AbstractSeq
Definition: AbstractSeq.h:21
cnoid::BodyMotion::setNumParts
void setNumParts(int numJoints, bool clearNewElements=false)
Definition: BodyMotion.h:141
cnoid::BodyMotion::jointPosSeq
std::shared_ptr< MultiValueSeq > jointPosSeq()
Definition: BodyMotion.h:57
cnoid::BodyMotion::Frame::~Frame
~Frame()
Definition: BodyMotion.h:73
cnoid::BodyMotion::extraSeqEnd
ConstSeqIterator extraSeqEnd() const
Definition: BodyMotion.h:106
cnoid::BodyMotion::getNumParts
int getNumParts() const
Definition: BodyMotion.h:146
cnoid::Signal
Definition: Signal.h:165
cnoid::BodyMotion::ConstFrame::ConstFrame
ConstFrame(const ConstFrame &org)
Definition: BodyMotion.h:87
cnoid::SignalProxy
Definition: Signal.h:470
cnoid::BodyMotion::numLinks
int numLinks() const
Definition: BodyMotion.h:33
cnoid::BodyMotion::extraSeq
std::shared_ptr< SeqType > extraSeq(const std::string &name) const
Definition: BodyMotion.h:109
cnoid::operator>>
CNOID_EXPORT EasyScanner & operator>>(EasyScanner &scanner, double &value)
Definition: EasyScanner.cpp:681
cnoid::BodyMotion::linkPosSeq
std::shared_ptr< MultiSE3Seq > linkPosSeq()
Definition: BodyMotion.h:49
cnoid::BodyMotion::ConstFrame::frame
int frame() const
Definition: BodyMotion.h:91
cnoid::BodyMotion::jointPosSeq
std::shared_ptr< const MultiValueSeq > jointPosSeq() const
Definition: BodyMotion.h:61
cnoid::BodyMotion::ExtraSeqMap
std::map< std::string, std::shared_ptr< AbstractSeq > > ExtraSeqMap
Definition: BodyMotion.h:102
cnoid::BodyMotion::linkPosSeq
std::shared_ptr< const MultiSE3Seq > linkPosSeq() const
Definition: BodyMotion.h:53