00001
00002 #ifndef INCLUDED_DynamicMotionSequenceMC_h_
00003 #define INCLUDED_DynamicMotionSequenceMC_h_
00004
00005 #include "MotionSequenceMC.h"
00006 #include <vector>
00007
00008
00009
00010 class DynamicMotionSequence : public MotionSequence {
00011 public:
00012
00013 DynamicMotionSequence() : MotionSequence(), moves(), erased() {clear();}
00014
00015 explicit DynamicMotionSequence(const char* filename) : MotionSequence(), moves(), erased() {clear();LoadFile(filename);setPlayTime(1);}
00016
00017 virtual ~DynamicMotionSequence() {}
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 virtual int updateOutputs() {
00034 MotionSequence::updateOutputs();
00035 if(!isPlaying()) {
00036 for(unsigned int i=0; i<NumOutputs; i++)
00037 motman->setOutput(this,i,getOutputCmd(i));
00038 } else {
00039 for(unsigned int i=0; i<NumOutputs; i++) {
00040 Move_idx_t prev=prevs[i],next=nexts[i];
00041 OutputCmd frames[NumFrames];
00042 frames[0]=getOutputCmd(i);
00043 for(unsigned int t=playtime+FrameTime,j=1;j<NumFrames;j++,t+=FrameTime) {
00044 setRange(t,prev,next);
00045 if(next!=invalid_move)
00046 calcOutput(frames[j],t,moves[prev],moves[next]);
00047 else
00048 frames[j].unset();
00049 }
00050 motman->setOutput(this,i,frames);
00051 }
00052 }
00053 return NumOutputs;
00054
00055
00056 }
00057
00058 virtual void clear() {
00059 moves.clear();
00060 erased.clear();
00061 for(unsigned int i=0; i<NumOutputs; i++) {
00062 moves.push_back(Move());
00063 moves.back().cmd.unset();
00064 moves.back().next=invalid_move;
00065 moves.back().prev=invalid_move;
00066 prevs[i]=starts[i]=moves.size()-1;
00067 nexts[i]=invalid_move;
00068 }
00069 setPlayTime(1);
00070 }
00071 virtual unsigned int getMaxFrames() const { return -1U; }
00072 virtual unsigned int getUsedFrames() const { return moves.size()-erased.size(); }
00073
00074 protected:
00075
00076 typedef std::vector<Move> list_t;
00077
00078
00079 list_t moves;
00080 std::vector<Move_idx_t> erased;
00081
00082 virtual Move& getKeyFrame(Move_idx_t x) { return moves[x]; }
00083 virtual const Move& getKeyFrame(Move_idx_t x) const { return moves[x]; }
00084 virtual Move_idx_t newKeyFrame() {
00085 if(erased.empty()) {
00086 moves.push_back(Move());
00087 return moves.size()-1;
00088 } else {
00089 Move_idx_t x=erased.back();
00090 erased.pop_back();
00091 return x;
00092 }
00093 }
00094 virtual void eraseKeyFrame(Move_idx_t x) { erased.push_back(x); }
00095 void setRange(unsigned int t,Move_idx_t& prev, Move_idx_t& next) const {
00096 if(next!=invalid_move && moves[next].starttime<=t) {
00097 do {
00098 prev=next;
00099 next=moves[prev].next;
00100 } while(next!=invalid_move && moves[next].starttime<=t);
00101 } else if(t<moves[prev].starttime) {
00102 do {
00103 next=prev;
00104 prev=moves[next].prev;
00105 } while(t<moves[prev].starttime);
00106 }
00107 }
00108 };
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121 #endif