SsUtils.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00030 
00034 
00036 /* @{ */
00037 
00038 #ifndef __SS_UTILS_H_
00039 #define __SS_UTILS_H_
00040 
00041 #ifdef MIP_HOST_APPLE
00042 #include <applePatch.h>
00043 #endif
00044 
00045 #include <stdio.h>
00046 #include <math.h>
00047 #include <vector>
00048 #include <iostream>
00049 #include <string>
00050 #include <sstream>
00051 #include <sys/time.h>
00052 #include <sys/select.h>
00053 #include <assert.h>
00054 #include <pthread.h>
00055 
00056 #include <MIPMatrix.h>
00057 // #include "stdRobot.h"
00058 #include <SE2.h>
00059 #include <Scan.h>
00060 #include <Time.h>
00061 
00062 
00063 using namespace std;
00064 
00065 namespace MipBaselib {
00069 class SsUtils{
00070  private:
00071  public:
00073   static bool encrypt(int &num, stringstream &ss){
00074    ss << num;
00075    return true;
00076   }
00077   
00079   static bool decrypt(int &num, stringstream &ss){
00080    if (ss.fail()) {
00081     cout << "WARNING: bad double stringstream." << endl;
00082     return false;
00083    }
00084    ss >> num;
00085    return true;
00086   }
00087   
00089   static bool encrypt(UCoordmm &num, stringstream &ss){
00090    ss << num;
00091    return true;
00092   }
00093   
00095   static bool decrypt(UCoordmm &num, stringstream &ss){
00096    if (ss.fail()) {
00097     cout << "WARNING: bad double stringstream." << endl;
00098     return false;
00099    }
00100    ss >> num;
00101    return true;
00102   }
00103   
00105   static bool encrypt(Decimal &num, stringstream &ss){
00106    ss << num;
00107    return true;
00108   }
00109   
00111   static bool decrypt(Decimal &num, stringstream &ss){
00112    if (ss.fail()) {
00113     cout << "WARNING: bad double stringstream." << endl;
00114     return false;
00115    }
00116    ss >> num;
00117    return true;
00118   }
00119   
00121   static bool encrypt(Decimal &num1, Decimal &num2, stringstream &ss){
00122    ss << num1 << " " << num2;
00123    return true;
00124   }
00125   
00127   static bool decrypt(Decimal &num1, Decimal &num2, stringstream &ss){
00128    if (ss.fail()) {
00129     cout << "WARNING: bad double stringstream, no first double." << endl;
00130     return false;
00131    }
00132    ss >> num1;
00133    if (ss.fail()) {
00134     cout << "WARNING: bad double stringstream, no second double." << endl;
00135     return false;
00136    }
00137    ss >> num2;
00138    return true;
00139   }
00140   
00142   static bool encrypt(Position &pos, stringstream &ss){
00143    ss << pos.x() << " " << pos.y();
00144    return true;
00145   }
00146   
00148   static bool decrypt(Position &pos, stringstream &ss){
00149    if (ss.fail()) {
00150     cout << "WARNING: bad double stringstream, no first double." << endl;
00151     return false;
00152    }
00153    Decimal x, y;
00154    ss >> x;
00155    if (ss.fail()) {
00156     cout << "WARNING: bad double stringstream, no second double." << endl;
00157     return false;
00158    }
00159    ss >> y;
00160    pos = Position(x, y);
00161    return true;
00162   }
00163   
00165   static bool encrypt(Pose &pose, stringstream &ss){
00166    ss << pose.pos().x() << " " << pose.pos().y() << " " << pose.ori().dCast2Pi();
00167    return true;
00168   }
00169   
00171   static bool decrypt(Pose &pose, stringstream &ss){
00172    if (ss.fail()) {
00173     cout << "WARNING: bad Pose stringstream, no x." << endl;
00174     return false;
00175    }
00176    double x, y, th;
00177    ss >> x;
00178    if (ss.fail()) {
00179     cout << "WARNING: bad Pose stringstream, no y." << endl;
00180     return false;
00181    }
00182    ss >> y;
00183    if (ss.fail()) {
00184     cout << "WARNING: bad Pose stringstream, no theta." << endl;
00185     return false;
00186    }
00187    ss >> th;
00188    pose = Pose(Position(x, y), th);
00189    return true;
00190   }
00191   
00193   static bool encrypt(PosiFeatures &feat, stringstream &ss){
00194    int size = feat.size();
00195    ss << size;
00196    for (int i = 0; i < size; i++){
00197     ss << " " << feat[i].getPosition().x() << " " << feat[i].getPosition().y() << " ";
00198     if (feat[i].associated()){
00199      ss << feat[i].getId();
00200     }
00201     else{
00202      ss << "na";
00203     }
00204    }
00205    return true;
00206   }
00207   
00209   static bool decrypt(PosiFeatures &feat, stringstream &ss){
00210    if (ss.fail()) {
00211     cout << "WARNING: bad PosiFeatures stringstream, no size." << endl;
00212     return false;
00213    }
00214    int size;
00215    ss >> size;
00216    for (int i = 0; i < size; i++){
00217     Decimal x, y;
00218     // Variable ID not used
00219 //     int id;
00220     if (ss.fail()) {
00221      cout << "WARNING: received broken setPosiFeatures message" << endl;
00222      return false;
00223     }
00224     ss >> x;
00225     if (ss.fail()) {
00226      cout << "WARNING: received broken setPosiFeatures message" << endl;
00227      return false;
00228     }
00229     ss >> y;
00230     if (ss.fail()) {
00231      cout << "WARNING: received broken setPosiFeatures message" << endl;
00232      return false;
00233     }
00234     string pid;
00235     ss >> pid;
00236     if (pid == "na"){
00237      feat.push_back(PosiFeature(Position(x, y)));
00238     }
00239     else{
00240      int id = atoi(pid.c_str());
00241      feat.push_back(PosiFeature(Position(x, y), id));
00242     }
00243    }
00244    return true;
00245   }
00246   
00248   static bool encrypt(Time &time, stringstream &ss){
00249    ss << time.sec() << " " << time.usec();
00250    return true;
00251   }
00252   
00254   static bool decrypt(Time &time, stringstream &ss){
00255    if (ss.fail()) {
00256     cout << "WARNING: bad time stringstream, no secs." << endl;
00257     return false;
00258    }
00259    long int sec, usec;
00260    ss >> sec;
00261    if (ss.fail()) {
00262     cout << "WARNING: bad time stringstream, no usecs." << endl;
00263     return false;
00264    }
00265    ss >> usec;
00266    time = Time(sec, usec);
00267    return true;
00268   }
00269   
00271   static bool encrypt(Ray &ray, stringstream &ss){
00272    ss << ray.reading() << " " << ray.bearing().dCast2Pi() << " " << ray.valid();
00273    return true;
00274   }
00275   
00277   static bool decrypt(Ray &ray, stringstream &ss){
00278    bool valid;
00279 //    Decimal bearing, reading;
00280    int bearing, reading;
00281    if (ss.fail()) {
00282     cout << "WARNING: bad ray stringstream, no reading." << endl;
00283     return false;
00284    }
00285    ss >> reading;
00286    if (ss.fail()) {
00287     cout << "WARNING: bad ray stringstream, no bearing." << endl;
00288     return false;
00289    }
00290    ss >> bearing;
00291    if (ss.fail()) {
00292     cout << "WARNING: bad ray stringstream, no valid." << endl;
00293     return false;
00294    }
00295    ss >> valid;
00296    ray = Ray(reading/1000.0, Angle(bearing/1000.0), valid);
00297    return true;
00298   }
00299   
00301   static bool decrypt(Ray &ray, stringstream &ss, int rayNum){
00302    bool valid;
00303    
00304    Decimal bearing = rayNum * ( 0.006135923*2 )  + ( 384/2 ) * (  0.006135923*2  ) + 1.832595715;
00305    int reading;
00306    if (ss.fail()) {
00307     cout << "WARNING: bad ray stringstream, no reading." << endl;
00308     return false;
00309    }
00310    ss >> reading;
00311    if (ss.fail()) {
00312     cout << "WARNING: bad ray stringstream, no bearing." << endl;
00313     return false;
00314    }
00315    ss >> valid;
00316    ray = Ray(reading/1000.0, Angle(bearing), valid);
00317    return true;
00318   }
00319   
00321   static bool decrypt(Ray &ray, stringstream &ss, int rayNum, Decimal scale, bool getBearing);
00322   
00324   static bool encrypt(Scan &scan, stringstream &ss){
00325    int size;
00326    scan.getSize(size);
00327    ss << size << " ";
00328    Ray temp;
00329    for (int i = 0; i < size; i++){
00330     scan.getRay(temp, i);
00331     encrypt(temp, ss);
00332     ss << " ";
00333    }
00334    return true;
00335   }
00336   
00338   static bool decrypt(Scan &scan, stringstream &ss);
00339 };
00340 
00341 }; // end of namespace
00342 
00343 #endif
00344 
00345 
00346 
00347 /* @} */
00348 
00349 
00350 
00351 

Generated on Mon Feb 20 07:01:07 2017 for MIP by  doxygen 1.5.6