Scan.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 
00031 
00033 /* @{ */
00034 
00035 #ifndef __SCAN_H_
00036 #define __SCAN_H_
00037 
00038 #ifdef MIP_HOST_APPLE
00039 #include <applePatch.h>
00040 #endif
00041 
00042 #include <stdlib.h>
00043 #include <string>
00044 #include <sstream>
00045 #include <vector>
00046 #include <assert.h>
00047 #include <pthread.h>
00048 
00049 //BOOST SERIALIZATION
00050 #ifdef FOUND_BOOST_SER
00051  #include <boost/serialization/vector.hpp>
00052  #include <boost/archive/text_iarchive.hpp>
00053  #include <boost/archive/text_oarchive.hpp>
00054 #endif
00055 
00056 #include <Time.h> 
00057 #include <Spaces.h>
00058 #include <Thread.h>
00059 
00060 #include "ScanTypes.h"
00061 
00062 namespace MipBaselib {
00067 class Ray{
00068  private:
00069 #ifdef FOUND_BOOST_SER
00070   friend class boost::serialization::access;
00071   template<class Archive>
00072   void serialize(Archive & ar, const unsigned int version){
00073    ar & _reading;
00074    ar & _bearing;
00075    ar & _valid;
00076   }
00077 #endif
00078   
00079   Decimal _reading; 
00080   Angle _bearing; 
00081   bool   _valid;   
00082  public:
00084   Ray() {
00085    _reading = 0.0;
00086    _bearing = Angle(0.0);
00087    _valid   = false;
00088   };
00090   Ray(Decimal r,Angle b,bool v) {
00091    _reading = r;
00092    _bearing = b;
00093    _valid   = v;
00094   };
00096   Ray(const Ray& r) {
00097    _reading = r._reading;
00098    _bearing = r._bearing;
00099    _valid   = r._valid;
00100   };
00102   Ray& operator=(const Ray& r){
00103    if (this != &r){
00104     _reading = r._reading;
00105     _bearing = r._bearing;
00106     _valid    = r._valid;
00107    }
00108    return *this;
00109   }
00111   bool operator==(const Ray &a) const {
00112    if( (_reading == a._reading)&&(_bearing == a._bearing)&&(_valid == a._valid) )
00113     return true;
00114    else return false;
00115   }
00117   bool operator!=(const Ray &other) const {
00118    return !(*this == other);
00119   }
00121   Decimal reading () {
00122    return _reading;
00123   }
00125   Angle bearing () {
00126    return _bearing;
00127   }
00129   bool valid () {
00130    return _valid;
00131   }
00133   void setValid (bool v) {
00134    _valid = v;
00135   }
00138   string print(){
00139    stringstream s;
00140    s.precision(5);
00141    s.setf(ios::fixed,ios::floatfield);
00142    if(_valid)
00143     s << "[" << _reading << " m, " << _bearing.print() << ", valid]" ;
00144    else
00145     s << "[" << _reading << " m, " << _bearing.print() << ", invalid]" ;
00146    return s.str();
00147   }
00148 };
00149 
00150 static bool _compareByBearing(Ray first, Ray second){
00151  return (first.bearing().dCast2Pi()<second.bearing().dCast2Pi());
00152 }
00153 
00154 
00155 
00156 
00160 typedef vector<Ray> Rays;
00161 
00162 
00167 class Raymm{
00168  private:
00169 #ifdef FOUND_BOOST_SER
00170   friend class boost::serialization::access;
00171   template<class Archive>
00172   void serialize(Archive & ar, const unsigned int version){
00173    ar & _reading;
00174    ar & _bearing;
00175    ar & _valid;
00176   }
00177 #endif
00178 
00179   
00180     UCoordmm _reading; 
00181     Anglem _bearing; 
00182     bool   _valid;   
00183  public:
00185   Raymm() {
00186    _reading = 0;
00187    _bearing = Anglem(0);
00188    _valid   = false;
00189   };
00191   Raymm(UCoordmm r,Anglem b,bool v) {
00192    _reading = r;
00193    _bearing = b;
00194    _valid   = v;
00195   };
00197   Raymm(const Raymm& r) {
00198    _reading = r._reading;
00199    _bearing = r._bearing;
00200    _valid   = r._valid;
00201   };
00203   Raymm& operator=(const Raymm& r){
00204    if (this != &r){
00205     _reading = r._reading;
00206     _bearing = r._bearing;
00207     _valid    = r._valid;
00208    }
00209    return *this;
00210   }
00212   bool operator==(const Raymm &a) const {
00213    if( (_reading == a._reading)&&(_bearing == a._bearing)&&(_valid == a._valid) )
00214     return true;
00215    else return false;
00216   }
00218   bool operator!=(const Raymm &other) const {
00219    return !(*this == other);
00220   }
00222   UCoordmm reading () {
00223    return _reading;
00224   }
00226   Anglem bearing () {
00227    return _bearing;
00228   }
00230   bool valid () {
00231    return _valid;
00232   }
00234   void setValid (bool v) {
00235    _valid = v;
00236   }
00239   string print(){
00240    stringstream s;
00241    if(_valid)
00242     s << "[" << _reading << " mm, " << _bearing.print() << ", valid]" ;
00243    else
00244     s << "[" << _reading << " mm, " << _bearing.print() << ", invalid]" ;
00245    return s.str();
00246   }
00247 };
00248 
00249 
00250 
00254 typedef vector<Raymm> Raymms;
00255 
00256 
00257 
00283 class Scan{
00284  private:
00285 #ifdef FOUND_BOOST_SER
00286   friend class boost::serialization::access;
00287   template<class Archive>
00288   void serialize(Archive & ar, const unsigned int version){
00289    ar & _scanSize;
00290    ar & _rays;
00291    ar & _tStamp;
00292    ar & _valid;
00293   }
00294 #endif
00295     
00296   ScanTypes _scanType;
00297   
00298   Rays  _rays; 
00299   Raymms  _raymms;  
00300   Time _tStamp; 
00301   bool _valid; 
00302 
00303   EnhancedMutEx _eMutEx; 
00304   
00305   /*static constants*/
00306   static const int SCAN_RESERV_SIZE = 1024; 
00307 
00308   int _scanSize;
00309   
00310   //Decimal _coneAmplitude; ///<The amplitude of a cone, stored for efficiency.
00311   Ray* _maxBearingRay; 
00312   Ray* _minBearingRay;
00313 
00315   bool _computeAngRanges(){
00316    if(!_eMutEx.allowed()) return false;
00317     _maxBearingRay =  &(*_rays.rbegin());
00318     _minBearingRay = &(*_rays.begin());
00319    return true;
00320   };
00321   
00322  public:
00324   Scan(){
00325    _rays.reserve(SCAN_RESERV_SIZE);
00326    _valid = false;
00327    _scanType = SCAN_SENS;
00328    _scanSize=0;
00329    _maxBearingRay=0;
00330    _minBearingRay=0;
00331   }
00333   Scan(ScanTypes type){
00334    _rays.reserve(SCAN_RESERV_SIZE);
00335    _valid = false;
00336    _scanType = type;
00337 /*   cout << "type = " << type << endl;
00338    cout << "_scanType = " << _scanType << endl;*/
00339    _scanSize=0;
00340    _maxBearingRay=0;
00341    _minBearingRay=0;
00342   }
00346   Scan(Rays r,Time t);
00347   
00351   Scan(Raymms r,Time t);
00352   
00355   /*TODO verificare anche l'allowing su s*/
00356   Scan(const Scan &s);
00357   
00359   /*TODO verificare anche l'allowing su s*/
00360   Scan& operator=(const Scan& s){
00361 //    cout << pthread_self() << " DEBUG = 1" << endl;
00362    if(!_eMutEx.allowed() ){/*if not allowed reset*/
00363 //     cout << pthread_self()<< " DEBUG = 2" << endl;
00364     Scan nullScan;
00365     _scanType = nullScan._scanType;
00366     _rays     = nullScan._rays;
00367     _raymms   = nullScan._raymms;
00368     _tStamp   = nullScan._tStamp;
00369     _valid    = nullScan._valid;
00370     _scanSize = nullScan._scanSize;
00371     _maxBearingRay=nullScan._maxBearingRay;
00372     _minBearingRay=nullScan._minBearingRay;
00373    }
00374    if (this != &s){
00375 //     cout << pthread_self() << " DEBUG = 2bis" << endl;
00376     _scanType = s._scanType;
00377     _rays     = s._rays;
00378     _raymms   = s._raymms;
00379     _tStamp   = s._tStamp;
00380     _valid    = s._valid;
00381     _scanSize = s._scanSize;
00382     _maxBearingRay=s._maxBearingRay;
00383     _minBearingRay=s._minBearingRay;
00384    }
00385    return *this;
00386   }
00387   
00392   bool operator==(Scan &s) {
00393    
00394    if(!s._eMutEx.allowed()) return false;
00395    if(!_eMutEx.allowed()) return false;
00396    
00397    if (_scanType != s._scanType) return false;
00398    
00399    int scanSize;
00400    if(!getSize(scanSize)) return false;
00401    
00402    if (_scanType == SCAN_SENS){
00403     for(int i=0;i<scanSize;i++){
00404       Ray r,rOther;
00405       if(!getRay(r,i)) return false;
00406       if(!s.getRay(rOther,i)) return false;
00407       if (r != rOther) return false;
00408     }
00409    } else if (_scanType == SCAN_SENSMM){
00410     for(int i=0;i<scanSize;i++){
00411      Raymm r,rOther;
00412      if(!getRay(r,i)) return false;
00413      if(!s.getRay(rOther,i)) return false;
00414      if (r != rOther) return false;
00415     }
00416    }
00417    return true;
00418   }
00419   
00422   bool operator!=(Scan &s) {
00423    return !(*this == s);
00424   }
00425   
00429   bool getConeAmplitude(Decimal &coneAmplitude);
00430   
00434   bool getScanAngle(Decimal &scanAngle);
00435   
00436   /*two mandatory wrappers*/
00441   bool askExclusiveAccess(Time timeout){
00442    return _eMutEx.askExclusiveAccess(timeout);
00443   }
00444   
00446   void leaveExclusiveAccess(){
00447    _eMutEx.leaveExclusiveAccess();
00448   }
00449   
00452   bool allowed(){
00453    return _eMutEx.allowed();
00454   }
00455   
00459   bool reserve(int n){
00460    if(!_eMutEx.allowed()){
00461     return false;
00462    } else {
00463     if (_scanType == SCAN_SENS){
00464      _rays.reserve(n);
00465     }else if (_scanType == SCAN_SENSMM){
00466      _raymms.reserve(n);
00467     }else {
00468      return false;
00469     }
00470     return true;
00471    }
00472   }
00473   
00474   /*get functions*/
00478   bool valid(bool& validity){
00479    if(!_eMutEx.allowed()) return false;
00480    validity = _valid;
00481    return true;
00482   }
00483   
00487   bool getScanType(ScanTypes& type){
00488    type = _scanType;
00489    return true;
00490   }
00491   
00495   bool getSize(int& size);
00496 
00499   Angle getAngRes(){
00500    return (_rays[1].bearing()-_rays[0].bearing());
00501   };
00502   
00506   bool getTimeStamp(Time& timestamp){
00507    if(!_eMutEx.allowed()) return false;
00508    timestamp = _tStamp;
00509    return true;
00510   }
00511   
00516   bool getRay(Ray& ray, int index);
00517   
00522   bool getRay(Raymm& raymm, int index);
00523   
00528   bool setRay(Ray& ray, int index);
00529   
00534   bool setRay(Raymm& raymm, int index);
00535   
00539   bool setTimeStamp(Time t){
00540    if(!_eMutEx.allowed()) return false;
00541    _tStamp = t;
00542    return true;
00543   }
00548   bool addRay(const Ray r){
00549    if (_scanType == SCAN_SENS){
00550     if(!_eMutEx.allowed()) return false;
00551     _rays.push_back(r);
00552     _valid  = true;
00553     _scanSize++;
00554     _computeAngRanges();
00555     return true;
00556    } else return false;
00557   }
00558   
00563   bool addRay(const Raymm r){
00564    if (_scanType == SCAN_SENSMM){
00565     if(!_eMutEx.allowed()) return false;
00566     _raymms.push_back(r);
00567     _valid  = true;
00568     _scanSize++;
00569     _computeAngRanges();
00570     return true;
00571    } else return false;
00572   }
00573   
00576   bool clear(){
00577    if(!_eMutEx.allowed()) return false;
00578    _rays.clear();
00579    _raymms.clear();
00580    _valid  = false;
00581    _scanSize=0;
00582    _maxBearingRay = 0;
00583    _minBearingRay = 0;
00584    return true;
00585   }
00586   
00590   bool voidReadings(Decimal minReading,Decimal maxReading);
00591   
00594   string print();
00595   
00599   void printJson(fstream& s);
00600   
00605   UCoordmm unmutexedGetReading(int index){
00606    if (_scanType == SCAN_SENSMM) return _raymms[index].reading();
00607    else return 0;
00608   }
00609   
00614   bool unmutexedGetValid(int index){
00615    if (_scanType == SCAN_SENSMM) return _raymms[index].valid();
00616    else return false;
00617   }
00618   
00619   
00620 };
00621 
00622 }; // end of namespace
00623 
00624 
00625 #endif
00626 
00627 /* @} */
00628 

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