00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2009 University of Malaga | 00007 | | 00008 | This software was written by the Machine Perception and Intelligent | 00009 | Robotics Lab, University of Malaga (Spain). | 00010 | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> | 00011 | | 00012 | This file is part of the MRPT project. | 00013 | | 00014 | MRPT is free software: you can redistribute it and/or modify | 00015 | it under the terms of the GNU General Public License as published by | 00016 | the Free Software Foundation, either version 3 of the License, or | 00017 | (at your option) any later version. | 00018 | | 00019 | MRPT is distributed in the hope that it will be useful, | 00020 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 00021 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 00022 | GNU General Public License for more details. | 00023 | | 00024 | You should have received a copy of the GNU General Public License | 00025 | along with MRPT. If not, see <http://www.gnu.org/licenses/>. | 00026 | | 00027 +---------------------------------------------------------------------------+ */ 00028 #ifndef CRawlog_H 00029 #define CRawlog_H 00030 00031 #include <mrpt/poses/CPose2D.h> 00032 #include <mrpt/slam/CSensoryFrame.h> 00033 #include <mrpt/slam/CActionCollection.h> 00034 #include <mrpt/slam/CObservationComment.h> 00035 #include <mrpt/utils/CConfigFileMemory.h> 00036 00037 00038 namespace mrpt 00039 { 00040 namespace slam 00041 { 00042 DEFINE_SERIALIZABLE_PRE( CRawlog ) 00043 00044 using namespace mrpt::utils; 00045 00046 typedef std::pair<mrpt::system::TTimeStamp, CObservationPtr> TTimeObservationPair; //!< For usage with CRawlog classes. 00047 typedef std::multimap<mrpt::system::TTimeStamp, CObservationPtr> TListTimeAndObservations; //!< For usage with CRawlog classes. 00048 00049 00050 /** This class stores a rawlog (robotic datasets) in one of two possible formats: 00051 * - Format #1: A sequence of actions and observations. There is a sequence of objects, where each one can be of one type: 00052 * - An action: Implemented as a CActionCollection object, the actuation of the robot (i.e. odometry increment). 00053 * - Observations: Implemented as a CSensoryFrame, refering to a set of robot observations from the same pose. 00054 * - Format #2: A sequence of actions and observations. There is a sequence of objects, where each one can be of one type: 00055 * 00056 * Refer to the wiki page about <a href="http://babel.isa.uma.es/mrpt/index.php/Rawlog_Format">rawlog files</a>. 00057 * 00058 * See also the application <a href="http://babel.isa.uma.es/mrpt/index.php/Application:RawLogViewer" >RawLogViewer</a > for the GUI program that visualizes and manages rawlogs. 00059 * 00060 * This class also publishes a static helper method for loading rawlog files in format #1: see CRawlog::readActionObservationPair 00061 * 00062 * There is a field for comments and blocks of parameters (in ini-like format) accessible through getCommentText and setCommentText 00063 * (introduced in MRPT 0.6.4). When serialized to a rawlog file, the commens are saved as an additional observation of the 00064 * type CObservationComments at the beginning of the file, but this observation does not appear after loading for clarity. 00065 * 00066 * \note Since MRPT version 0.5.5, this class also provides a STL container-like interface (see CRawlog::begin, CRawlog::iterator, ...). 00067 * \note The format #2 is supported since MRPT version 0.6.0. 00068 * 00069 * \sa CSensoryFrame, CPose2D, <a href="http://babel.isa.uma.es/mrpt/index.php/Rawlog_Format">RawLog file format</a>. 00070 */ 00071 class MRPTDLLIMPEXP CRawlog : public mrpt::utils::CSerializable 00072 { 00073 // This must be added to any CSerializable derived class: 00074 DEFINE_SERIALIZABLE( CRawlog ) 00075 00076 private: 00077 typedef std::deque<CSerializablePtr> TListObjects; 00078 TListObjects m_seqOfActObs; //!< The list where the objects really are in. 00079 00080 CObservationComment m_commentTexts; //!< Comments of the rawlog. 00081 00082 public: 00083 void getCommentText( std::string &t) const; //!< Returns the block of comment text for the rawlog 00084 std::string getCommentText() const; //!< Returns the block of comment text for the rawlog 00085 void setCommentText( const std::string &t); //!< Changes the block of comment text for the rawlog 00086 void getCommentTextAsConfigFile( mrpt::utils::CConfigFileMemory &memCfg ) const; //!< Saves the block of comment text for the rawlog into the passed config file object. 00087 00088 /** The type of each entry in a rawlog. 00089 * \sa CRawlog::getType 00090 */ 00091 enum TEntryType 00092 { 00093 etSensoryFrame = 0, 00094 etActionCollection, 00095 etObservation 00096 }; 00097 00098 /** Default constructor: 00099 */ 00100 CRawlog(); 00101 00102 /** Copy operator: 00103 * \sa moveFrom 00104 */ 00105 // No need from introduction of smart pointers 00106 // CRawlog& operator =(const CRawlog &o); 00107 00108 /** Destructor: 00109 */ 00110 virtual ~CRawlog(); 00111 00112 /** Clear the sequence of actions/observations, freeing the memory of all the objects in the list. 00113 */ 00114 void clear(); 00115 00116 /** Clear the sequence of actions/observations, without deleting the objects themselves (USE ONLY IF YOU KNOW WHAT YOU DO, NORMALLY YOU'LL CALL "clear" INSTEAD). 00117 */ 00118 void clearWithoutDelete(); 00119 00120 /** Add an action to the sequence: a collection of just one element is created. 00121 * The object is duplicated, so the original one can be free if desired. 00122 */ 00123 void addAction( CAction &action ); 00124 00125 /** Add a set of actions to the sequence; the object is duplicated, so the original one can be free if desired. 00126 * \sa addObservations, addActionsMemoryReference 00127 */ 00128 void addActions( CActionCollection &action ); 00129 00130 /** Add a set of observations to the sequence; the object is duplicated, so the original one can be free if desired. 00131 * \sa addActions, addObservationsMemoryReference 00132 */ 00133 void addObservations( CSensoryFrame &observations ); 00134 00135 /** Add a set of actions to the sequence, using a smart pointer to the object to add. 00136 * \sa addActions, addObservationsMemoryReference, addObservationMemoryReference 00137 */ 00138 void addActionsMemoryReference( const CActionCollectionPtr &action ); 00139 00140 /** Add a set of observations to the sequence, using a smart pointer to the object to add. 00141 * \sa addObservations, addActionsMemoryReference, addObservationMemoryReference 00142 */ 00143 void addObservationsMemoryReference( const CSensoryFramePtr &observations ); 00144 00145 /** Add a single observation to the sequence, using a smart pointer to the object to add. 00146 * \sa addObservations, addActionsMemoryReference 00147 */ 00148 void addObservationMemoryReference( const CObservationPtr &observation ); 00149 00150 /** Load the contents from a file containing one of these possibilities: 00151 * - A "CRawlog" object. 00152 * - Directly "CSensoryFrame" and "CActionCollection" objects. In this case the method stops reading on EOF of an unrecogniced class name. 00153 * \returns It returns false if the file does not exists. 00154 */ 00155 bool loadFromRawLogFile( const std::string &fileName ); 00156 00157 /** Saves the contents to a rawlog-file, compatible with RawlogViewer (As the sequence of internal objects). 00158 * The file is saved with gz-commpressed is MRPT has gz-streams. 00159 * \returns It returns false if the file does not exists. 00160 */ 00161 bool saveToRawLogFile( const std::string &fileName ) const; 00162 00163 /** Returns the number of actions / observations object in the sequence. 00164 */ 00165 size_t size() const; 00166 00167 /** Returns the type of a given element. 00168 * \sa isAction, isObservation 00169 */ 00170 TEntryType getType( size_t index ) const; 00171 00172 /** Delete the action or observation stored in the given index. 00173 * \exception std::exception If index is out of bounds 00174 */ 00175 void remove( size_t index ); 00176 00177 /** Returns the i'th element in the sequence, as being actions, where index=0 is the first object. 00178 * If it is not a CActionCollection, it throws an exception. Do neighter modify nor delete the returned pointer. 00179 * \sa size, isAction, getAsObservations, getAsObservation 00180 * \exception std::exception If index is out of bounds 00181 */ 00182 CActionCollectionPtr getAsAction( size_t index ) const; 00183 00184 /** Returns the i'th element in the sequence, as being an action, where index=0 is the first object. 00185 * If it is not an CSensoryFrame, it throws an exception. Do neighter modify nor delete the returned pointer. 00186 * \sa size, isAction, getAsAction, getAsObservation 00187 * \exception std::exception If index is out of bounds 00188 */ 00189 CSensoryFramePtr getAsObservations( size_t index ) const; 00190 00191 /** Returns the i'th element in the sequence, being its class whatever. 00192 * \sa size, isAction, getAsAction, getAsObservations 00193 * \exception std::exception If index is out of bounds 00194 */ 00195 CSerializablePtr getAsGeneric( size_t index ) const; 00196 00197 /** Returns the i'th element in the sequence, as being an observation, where index=0 is the first object. 00198 * If it is not an CObservation, it throws an exception. Do neighter modify nor delete the returned pointer. 00199 * This is the proper method to obtain the objects stored in a "only observations"-rawlog file (named "format #2" above. 00200 * \sa size, isAction, getAsAction 00201 * \exception std::exception If index is out of bounds 00202 */ 00203 CObservationPtr getAsObservation( size_t index ) const; 00204 00205 00206 /** A normal iterator, plus the extra methods "isAction", "isObservation" to determine the type of each entry in the sequence. 00207 */ 00208 class iterator 00209 { 00210 protected: 00211 TListObjects::iterator m_it; 00212 00213 public: 00214 iterator() : m_it() { } 00215 iterator(const TListObjects::iterator& it) : m_it(it) { } 00216 virtual ~iterator() { } 00217 00218 iterator & operator = (const iterator& o) { m_it = o.m_it; return *this; } 00219 00220 bool operator == (const iterator& o) { return m_it == o.m_it; } 00221 bool operator != (const iterator& o) { return m_it != o.m_it; } 00222 00223 CSerializablePtr operator *() { return *m_it; } 00224 00225 iterator operator ++(int) { m_it++; return *this; } 00226 iterator operator --(int) { m_it--; return *this; } 00227 00228 TEntryType getType() const 00229 { 00230 if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CObservation) ) ) 00231 return etObservation; 00232 else if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CSensoryFrame) ) ) 00233 return etSensoryFrame; 00234 else 00235 return etActionCollection; 00236 } 00237 00238 static iterator erase( TListObjects& lst, const iterator &it) { return lst.erase(it.m_it); } 00239 }; 00240 00241 /** A normal iterator, plus the extra methods "isAction", "isObservation" to determine the type of each entry in the sequence. 00242 */ 00243 class const_iterator 00244 { 00245 protected: 00246 TListObjects::const_iterator m_it; 00247 00248 public: 00249 const_iterator() : m_it() { } 00250 const_iterator(const TListObjects::const_iterator& it) : m_it(it) { } 00251 virtual ~const_iterator() { } 00252 00253 bool operator == (const const_iterator& o) { return m_it == o.m_it; } 00254 bool operator != (const const_iterator& o) { return m_it != o.m_it; } 00255 00256 const CSerializablePtr operator *() const { return *m_it; } 00257 00258 const_iterator operator ++(int) { m_it++; return *this; } 00259 const_iterator operator --(int) { m_it--; return *this; } 00260 00261 TEntryType getType() const 00262 { 00263 if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CObservation) ) ) 00264 return etObservation; 00265 else if ( (*m_it)->GetRuntimeClass()->derivedFrom( CLASS_ID(CSensoryFrame) ) ) 00266 return etSensoryFrame; 00267 else 00268 return etActionCollection; 00269 } 00270 00271 }; 00272 00273 00274 const_iterator begin() const { return m_seqOfActObs.begin(); } 00275 iterator begin() { return m_seqOfActObs.begin(); } 00276 const_iterator end() const { return m_seqOfActObs.end(); } 00277 iterator end() { return m_seqOfActObs.end(); } 00278 00279 iterator erase(const iterator &it) { return iterator::erase(m_seqOfActObs, it); } 00280 00281 /** Returns the sub-set of observations of a given class whose time-stamp t fulfills time_start <= t < time_end. 00282 * This method requires the timestamps of the sensors to be in strict ascending order (which should be the normal situation). 00283 * Otherwise, the output is undeterminate. 00284 */ 00285 void findObservationsByClassInRange( 00286 mrpt::system::TTimeStamp time_start, 00287 mrpt::system::TTimeStamp time_end, 00288 const mrpt::utils::TRuntimeClassId *class_type, 00289 TListTimeAndObservations &out_found, 00290 size_t guess_start_position = 0 00291 ) const; 00292 00293 00294 /** Efficiently copy the contents from other existing object, and remove the data from the origin (after calling this, the original object will have no actions/observations). 00295 */ 00296 void moveFrom( CRawlog &obj); 00297 00298 /** Efficiently swap the contents of two existing objects. 00299 */ 00300 void swap( CRawlog &obj); 00301 00302 /** Reads a consecutive pair action / observation from the rawlog opened at some input stream. 00303 * Previous contents of action and observations are discarded (using stlplus::smart_ptr::clear_unique), and 00304 * at exit they contain the new objects read from the rawlog file. 00305 * The input/output variable "rawlogEntry" is just a counter of the last rawlog entry read, for logging or monitoring purposes. 00306 * \return false if there was some error, true otherwise. 00307 * \sa getActionObservationPair 00308 */ 00309 static bool readActionObservationPair( 00310 CStream &inStream, 00311 CActionCollectionPtr &action, 00312 CSensoryFramePtr &observations, 00313 size_t & rawlogEntry ); 00314 00315 /** Gets the next consecutive pair action / observation from the rawlog loaded into this object. 00316 * Previous contents of action and observations are discarded (using stlplus::smart_ptr::clear_unique), and 00317 * at exit they contain the new objects read from the rawlog file. 00318 * The input/output variable "rawlogEntry" is just a counter of the last rawlog entry read, for logging or monitoring purposes. 00319 * \return false if there was some error, true otherwise. 00320 * \sa readActionObservationPair 00321 */ 00322 bool getActionObservationPair( 00323 CActionCollectionPtr &action, 00324 CSensoryFramePtr &observations, 00325 size_t &rawlogEntry ) const; 00326 00327 00328 }; // End of class def. 00329 00330 } // End of namespace 00331 } // End of namespace 00332 00333 #endif
Page generated by Doxygen 1.5.9 for MRPT 0.7.1 SVN: at Mon Aug 17 22:21:34 EDT 2009 |