Documentation


history.h

Go to the documentation of this file.
00001 #ifndef ____________animal_history_hpp_____________
00002 #define ____________animal_history_hpp_____________
00003  
00004 #include <list> 
00005 
00010 namespace animal {
00011 
00021   template<class Position, class Velocity>
00022   class History
00023   {
00025     struct Key {
00026         double time;          
00027         Position position;    
00028         Velocity velocity;   
00029         
00031         Key(double t, Position p, Velocity v)
00032             : time(t)
00033             , position(p)
00034             , velocity(v)
00035         {} 
00036         
00038         Key(){}
00039     };
00040     
00041     typedef std::list< Key > List;         
00042     typename List::iterator cursor;        
00043     bool record;                           
00044     bool replay;                           
00045     
00046   public:
00048     History( double t, Position p, Velocity v, bool record = false )
00049       : record(record)
00050       , replay(false)
00051     {
00052       state_list.push_back( Key(t,p,v) );
00053       cursor = state_list.begin();
00054     }
00055     
00057     virtual ~History(){}
00058     
00060     void push_back( double t, Position p, Velocity v ){     
00061         if( isRecording() ){
00062             typename List::iterator c = cursor;
00063             c++;
00064             state_list.erase( c, state_list.end() );            
00065             state_list.push_back( Key(t,p,v) );
00066             cursor++;
00067         }
00068     }
00069     
00071     void reset(){ cursor = state_list.begin(); }
00072     
00074     void operator ++ () { 
00075         if( isReplaying() ){
00076             //cerr<<"history++, t= "<< cursor->first << " to ";
00077             cursor++;
00078             if( cursor==state_list.end() ) { 
00079                 cursor--; 
00080                 //cerr<<"END "; 
00081             }
00082             //cerr<< cursor->first << endl;
00083         }
00084     }
00085     
00087     void operator -- () { 
00088         if( isReplaying() ){
00089             if( cursor!=state_list.begin() ) {
00090                 cursor--; 
00091             }
00092         }
00093     }
00094     
00096     double t() const { return cursor->time; }
00097     
00099     void copyStateTo( Position* p, Velocity* v ) const { 
00100         *p = cursor->position; 
00101         *v = cursor->velocity;
00102     }
00103     
00105     void toggleRecord( bool b ){ 
00106         record = b; 
00107         if( record ) 
00108             replay = false; 
00109     }
00111     bool isRecording() const { return record; }
00112     
00114     void toggleReplay( bool b ){ 
00115         replay = b; 
00116         if( replay ) 
00117             record = false; 
00118     }
00119 
00121     bool isReplaying() const { return replay; }
00122     
00124     virtual void write( std::ostream& out ) const = 0;
00125     
00127     virtual void read( std::istream& in ) = 0;
00128 
00129 protected:
00130     List state_list;   
00131 
00132 
00133     
00134   };
00135 
00136 
00137 
00141   template<class Position, class Velocity>
00142   class HistoryS: public History<Position,Velocity>
00143   {
00144 
00145     public:
00146     
00147     typedef History<Position,Velocity> Parent;  
00148     typedef typename Parent::List List;                  
00149     
00151     HistoryS( double t, Position p, Velocity v, bool record = false )
00152       : History<Position,Velocity>(t,p,v,record)
00153     {}
00154     
00156     virtual void write( std::ostream& out ) const {
00157         typename List::const_iterator i, iend;
00158         for( i=this->state_list.begin(), iend=this->state_list.end(); i!=iend; ++i ){ 
00159             out << (*i).time << "\t" << (*i).position << "\t" << (*i).velocity << endl;
00160         }   
00161     }
00162     
00164     virtual void read( std::istream& in )
00165     {
00166         this->reset();
00167         this->toggleRecord(true);
00168         typename List::value_type key; 
00169         while( in >> key.time ){
00170             in >> key.position >> key.velocity;
00171             push_back( key.time, key.position, key.velocity );
00172         }
00173         if( !this->state_list.empty() )
00174             this->state_list.erase(this->state_list.begin()); // avoid duplication of time 0
00175         this->reset();
00176      }
00177 
00178 
00179   };
00180 
00181 
00185   template<class Position,class Velocity>
00186   class HistoryV: public History<Position,Velocity>
00187   {
00188 
00189   public:
00190   
00191     typedef History<Position,Velocity> Parent;  
00192     typedef typename Parent::List List;                  
00193     
00195     HistoryV( double t, Position p, Velocity v, bool record = false )
00196       : History<Position,Velocity>(t,p,v,record)
00197     {}
00198     
00200     virtual void write( std::ostream& out ) const {
00201         typename List::const_iterator i, iend;
00202         for( i=this->state_list.begin(), iend=this->state_list.end(); i!=iend; ++i ){ 
00203             out << (*i).time << "\t" << v_output((*i).position) << "\t" << v_output((*i).velocity) << endl;
00204         }   
00205     }
00206     
00208     virtual void read( std::istream& in )
00209     {
00210       this->reset();
00211       this->toggleRecord(true);
00212       typename List::value_type key; 
00213       while( in >> key.time ){
00214         key.position.resize( this->state_list.begin()->position.size() );
00215         key.velocity.resize( this->state_list.begin()->velocity.size() );
00216         in >> v_input(key.position);
00217         in >> v_input(key.velocity);
00218         push_back( key.time, key.position, key.velocity );
00219       }
00220       if( !this->state_list.empty() )
00221             this->state_list.erase(this->state_list.begin()); // avoid duplication of time 0
00222       this->reset();
00223     }
00224 
00225   };
00226 
00227 }//animal
00228 
00229 #endif

Generated on Thu Dec 23 13:52:25 2004 by doxygen 1.3.6