00001 #include "CarmenLog.h"
00002
00003 #include <boost/algorithm/string/predicate.hpp>
00004 #include <math.h>
00005
00006 void CarmenLogReader::readLog(std::istream& _stream, std::vector<AbstractReading*>& _log) const{
00007 char buffer[MAX_LINE_SIZE];
00008
00009 while(_stream){
00010 _stream.getline(buffer,MAX_LINE_SIZE);
00011 std::istringstream instream(buffer);
00012 AbstractReading *reading = readLine(instream);
00013 if (reading)
00014 _log.push_back(reading);
00015 }
00016 }
00017
00018 AbstractReading* CarmenLogReader::readLine(std::istream& _stream) const{
00019 std::string sensorName;
00020 _stream >> sensorName;
00021 _stream.seekg(0, std::ios_base::beg);
00022
00023 if (boost::iequals(sensorName,"FLASER")){
00024 return parseFLaser(_stream);
00025 } else if (boost::istarts_with(sensorName,"ROBOTLASER")){
00026 return parseRobotLaser(_stream);
00027 } else if (boost::istarts_with(sensorName,"RAWLASER")){
00028 return parseRawLaser(_stream);
00029 }
00030 return 0;
00031 }
00032
00033 LaserReading* CarmenLogReader::parseRobotLaser(std::istream& _stream) const{
00034 std::string sensorName, robotName;
00035 std::vector<double> phi,rho,remission;
00036 unsigned int number=0, remissionNumber=0;
00037 OrientedPoint2D laserPose, robotPose;
00038 double timestamp;
00039 double start, fov, resolution, maxRange, accuracy;
00040 int laserType, remissionMode;
00041 double tv, rv, forward_safety_dist, side_safety_dist, turn_axis;
00042
00043 _stream >> sensorName >> laserType >> start >> fov >> resolution >> maxRange >> accuracy >> remissionMode;
00044
00045 _stream >> number;
00046 phi.resize(number);
00047 rho.resize(number);
00048
00049 for(uint i=0; i < number; i++){
00050 phi[i] = start + i*resolution;
00051 _stream >> rho[i];
00052 }
00053
00054 _stream >> remissionNumber;
00055 remission.resize(remissionNumber);
00056
00057 for(uint i=0; i<remissionNumber; i++){
00058 _stream >> remission[i];
00059 }
00060
00061 _stream >> laserPose.x >> laserPose.y >> laserPose.theta;
00062 _stream >> robotPose.x >> robotPose.y >> robotPose.theta;
00063
00064 _stream >> tv >> rv >> forward_safety_dist >> side_safety_dist >> turn_axis;
00065
00066 _stream >> timestamp >> robotName;
00067
00068 LaserReading *result = new LaserReading(phi, rho, timestamp, sensorName, robotName);
00069 result->setMaxRange(maxRange);
00070 result->setRemission(remission);
00071 result->setLaserPose(laserPose);
00072
00073 return result;
00074 }
00075
00076 LaserReading* CarmenLogReader::parseRawLaser(std::istream& _stream) const{
00077 std::string sensorName, robotName;
00078 std::vector<double> phi,rho,remission;
00079 unsigned int number, remissionNumber;
00080 OrientedPoint2D laserPose, robotPose;
00081 double timestamp;
00082 double start, fov, resolution, maxRange, accuracy;
00083 int laserType, remissionMode;
00084
00085 _stream >> sensorName >> laserType >> start >> fov >> resolution >> maxRange >> accuracy >> remissionMode;
00086
00087 _stream >> number;
00088 phi.resize(number);
00089 rho.resize(number);
00090
00091 for(uint i=0; i < number; i++){
00092 phi[i] = start + i*resolution;
00093 _stream >> rho[i];
00094 }
00095
00096 _stream >> remissionNumber;
00097 remission.resize(remissionNumber);
00098
00099 for(uint i=0; i<remissionNumber; i++){
00100 _stream >> remission[i];
00101 }
00102
00103 _stream >> timestamp >> robotName;
00104
00105 LaserReading *result = new LaserReading(phi, rho, timestamp, sensorName, robotName);
00106 result->setMaxRange(maxRange);
00107 result->setRemission(remission);
00108
00109
00110 return result;
00111 }
00112
00113 LaserReading* CarmenLogReader::parseFLaser(std::istream& _stream) const{
00114 std::string sensorName, robotName;
00115 std::vector<double> phi,rho;
00116 unsigned int number;
00117 OrientedPoint2D laserPose, robotPose;
00118 double timestamp;
00119 double start, fov, resolution, maxRange;
00120
00121 _stream >> sensorName >> number;
00122
00123 phi.resize(number);
00124 rho.resize(number);
00125 start = -M_PI_2;
00126 fov = M_PI;
00127 resolution = fov/number;
00128 maxRange = 81.9;
00129
00130 for(uint i=0; i<number; i++){
00131 phi[i] = start + i*resolution;
00132 _stream >> rho[i];
00133 }
00134
00135 _stream >> laserPose.x >> laserPose.y >> laserPose.theta;
00136 _stream >> robotPose.x >> robotPose.y >> robotPose.theta;
00137
00138 _stream >> timestamp >> robotName;
00139
00140 LaserReading *result = new LaserReading(phi, rho, timestamp, sensorName, robotName);
00141 result->setMaxRange(maxRange);
00142 result->setLaserPose(laserPose);
00143
00144 return result;
00145 }
00146
00147
00148 void CarmenLogWriter::writeLog(std::ostream& _stream, const std::vector<AbstractReading*>& _log) const{
00149 for(uint i = 0; i < _log.size(); i++){
00150 writeLine(_stream, _log[i]);
00151 }
00152 }
00153
00154 void CarmenLogWriter::writeLine(std::ostream& _stream, const AbstractReading* _reading) const{
00155 if (boost::iequals(_reading->getName(),"FLASER")){
00156 writeFLaser(_stream, dynamic_cast<const LaserReading*>(_reading));
00157 } else if (boost::istarts_with(_reading->getName(),"ROBOTLASER")){
00158 writeRobotLaser(_stream, dynamic_cast<const LaserReading*>(_reading));
00159 } else if (boost::istarts_with(_reading->getName(),"RAWLASER")){
00160 writeRawLaser(_stream, dynamic_cast<const LaserReading*>(_reading));
00161 }
00162 }
00163
00164 void CarmenLogWriter::writeFLaser(std::ostream& _stream, const LaserReading* _reading) const{
00165 _stream << std::fixed;
00166 _stream << _reading->getName() << " ";
00167
00168 const std::vector<double>& rho = _reading->getRho();
00169 _stream << rho.size() << " ";
00170
00171 _stream.precision(3);
00172 for(uint i = 0; i < rho.size(); i++){
00173 _stream << rho[i] << " ";
00174 }
00175
00176 const OrientedPoint2D &laserPose = _reading->getLaserPose();
00177
00178 _stream.precision(6);
00179 _stream << laserPose.x << " " << laserPose.y << " " << laserPose.theta << " ";
00180 _stream << laserPose.x << " " << laserPose.y << " " << laserPose.theta << " ";
00181
00182 _stream << _reading->getTime() << " " << _reading->getRobot() << " " << _reading->getTime() << std::endl;
00183 }
00184
00185 void CarmenLogWriter::writeRobotLaser(std::ostream& _stream, const LaserReading* _reading) const{
00186 _stream << std::fixed;
00187 _stream << _reading->getName() << " " << "0 ";
00188
00189 const std::vector<double>& rho = _reading->getRho();
00190 const std::vector<double>& phi = _reading->getPhi();
00191
00192 _stream.precision(6);
00193 _stream << phi.front() << " " << phi.back() - phi.front() << " " << (phi[1] - phi[0]) << " " << _reading->getMaxRange() << " " << "0.010000 "<< "0 ";
00194
00195 _stream << rho.size() << " ";
00196
00197 _stream.precision(3);
00198 for(uint i = 0; i < rho.size(); i++){
00199 _stream << rho[i] << " ";
00200 }
00201
00202 const std::vector<double>& remission = _reading->getRemission();
00203 _stream << remission.size() << " ";
00204
00205 for(uint i = 0; i < remission.size(); i++){
00206 _stream << remission[i] << " ";
00207 }
00208
00209 const OrientedPoint2D &laserPose = _reading->getLaserPose();
00210
00211 _stream.precision(6);
00212 _stream << laserPose.x << " " << laserPose.y << " " << laserPose.theta << " ";
00213 _stream << laserPose.x << " " << laserPose.y << " " << laserPose.theta << " ";
00214
00215 _stream << 0.0 << " " << 0.0 << " " << 0.0 << " " << 0.0 << " " << 0.0 << " ";
00216
00217 _stream << _reading->getTime() << " " << _reading->getRobot() << " " << _reading->getTime() << std::endl;
00218 }
00219
00220 void CarmenLogWriter::writeRawLaser(std::ostream& _stream, const LaserReading* _reading) const{
00221 _stream << std::fixed;
00222 _stream << _reading->getName() << " " << "0 ";
00223
00224 const std::vector<double>& rho = _reading->getRho();
00225 const std::vector<double>& phi = _reading->getPhi();
00226
00227 _stream.precision(6);
00228 _stream << phi.front() << " " << phi.back() - phi.front() << " " << (phi[1] - phi[0]) << " " << _reading->getMaxRange() << " " << "0.010000 "<< "0 ";
00229
00230 _stream << rho.size() << " ";
00231
00232 _stream.precision(3);
00233 for(uint i = 0; i < rho.size(); i++){
00234 _stream << rho[i] << " ";
00235 }
00236
00237 const std::vector<double>& remission = _reading->getRemission();
00238 _stream << remission.size() << " ";
00239
00240 for(uint i = 0; i < remission.size(); i++){
00241 _stream << remission[i] << " ";
00242 }
00243
00244 _stream << _reading->getTime() << " " << _reading->getRobot() << " " << _reading->getTime() << std::endl;
00245 }