DynamicMotionSequence.h
Go to the documentation of this file.00001
00002 #ifndef INCLUDED_DynamicMotionSequence_h_
00003 #define INCLUDED_DynamicMotionSequence_h_
00004
00005 #include "MotionSequenceEngine.h"
00006 #include "MotionCommand.h"
00007 #include "MotionManager.h"
00008 #include "Events/EventBase.h"
00009 #include <vector>
00010
00011
00012
00013 class DynamicMotionSequence : public MotionCommand, public MotionSequenceEngine {
00014 public:
00015
00016
00017 DynamicMotionSequence() : MotionCommand(), MotionSequenceEngine(), moves(), erased(), dirty(false) { clear(); }
00018
00019
00020 explicit DynamicMotionSequence(const char* filename) : MotionCommand(), MotionSequenceEngine(), moves(), erased(), dirty(false) {
00021 clear();
00022 loadFile(filename);
00023 setTime(1);
00024 }
00025
00026
00027 virtual ~DynamicMotionSequence() {}
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 virtual int isDirty() { return isPlaying(); }
00044 virtual int isAlive() { return (playspeed>0) ? (playtime<=endtime) : (playtime>0); }
00045
00046 void setDirty() { dirty = true; };
00047
00048 virtual int updateOutputs() {
00049 MotionSequenceEngine::updateOutputs();
00050 if(!isPlaying()) {
00051 if(dirty)
00052 postEvent(EventBase(EventBase::motmanEGID,getID(),EventBase::statusETID));
00053 dirty=false;
00054 for(unsigned int i=0; i<NumOutputs; i++)
00055 motman->setOutput(this,i,getOutputCmd(i));
00056 } else {
00057 dirty=true;
00058 for(unsigned int i=0; i<NumOutputs; i++) {
00059 Move_idx_t prev=prevs[i],next=nexts[i];
00060 OutputCmd frames[NumFrames];
00061 frames[0]=getOutputCmd(i);
00062 for(unsigned int t=playtime+FrameTime,j=1;j<NumFrames;j++,t+=FrameTime) {
00063 setRange(t,prev,next);
00064 if(next!=invalid_move)
00065 calcOutput(frames[j],t,moves[prev],moves[next]);
00066 else
00067 frames[j].unset();
00068 }
00069 motman->setOutput(this,i,frames);
00070 }
00071 }
00072 return NumOutputs;
00073
00074
00075 }
00076
00077 virtual void clear() {
00078 moves.clear();
00079 erased.clear();
00080 for(unsigned int i=0; i<NumOutputs; i++) {
00081 moves.push_back(Move());
00082 moves.back().cmd.unset();
00083 moves.back().next=invalid_move;
00084 moves.back().prev=invalid_move;
00085 prevs[i]=starts[i]=moves.size()-1;
00086 nexts[i]=invalid_move;
00087 }
00088 setTime(1);
00089 }
00090 virtual unsigned int getMaxFrames() const { return -1U; }
00091 virtual unsigned int getUsedFrames() const { return moves.size()-erased.size(); }
00092
00093 protected:
00094
00095 typedef std::vector<Move> list_t;
00096
00097
00098 list_t moves;
00099 std::vector<Move_idx_t> erased;
00100 bool dirty;
00101
00102 virtual Move& getKeyFrame(Move_idx_t x) { return moves[x]; }
00103 virtual const Move& getKeyFrame(Move_idx_t x) const { return moves[x]; }
00104 virtual Move_idx_t newKeyFrame() {
00105 if(erased.empty()) {
00106 moves.push_back(Move());
00107 return moves.size()-1;
00108 } else {
00109 Move_idx_t x=erased.back();
00110 erased.pop_back();
00111 return x;
00112 }
00113 }
00114
00115 virtual void eraseKeyFrame(Move_idx_t x) { erased.push_back(x); }
00116
00117 bool setRange(unsigned int t,Move_idx_t& prev, Move_idx_t& next) const {
00118 bool moved=false;
00119 if(next!=invalid_move && moves[next].starttime<=t) {
00120 moved=true;
00121 do {
00122 prev=next;
00123 next=moves[prev].next;
00124 } while(next!=invalid_move && moves[next].starttime<=t);
00125 } else {
00126 while(moves[prev].prev!=invalid_move && t<moves[prev].starttime) {
00127 next=prev;
00128 prev=moves[next].prev;
00129 moved=true;
00130 }
00131 }
00132 return moved;
00133 }
00134 };
00135
00136
00137
00138
00139
00140
00141 #endif