S1.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 
00032 /* @{ */
00033 
00034 
00035 #ifndef __S1_H_
00036 #define __S1_H_
00037 
00038 #ifdef MIP_HOST_APPLE
00039 #include <applePatch.h>
00040 #endif
00041 
00042 #include <stdio.h>
00043 #include <math.h>
00044 #include <vector>
00045 #include <iostream>
00046 #include <string>
00047 #include <sstream>
00048 #include <sys/time.h>
00049 #include <sys/select.h>
00050 #include <assert.h>
00051 
00052 #ifdef FOUND_BOOST_SER
00053  #include <boost/serialization/vector.hpp>
00054  #include <boost/archive/text_iarchive.hpp>
00055  #include <boost/archive/text_oarchive.hpp>
00056 #endif
00057 
00058 #include <Types.h>
00059 #include <numTools.h>
00060 
00061 using namespace std;
00062 
00066 class S1{
00067  private:
00068   Decimal _value;   
00069   Decimal _halfModule; 
00070   Decimal _module;   
00073   Decimal normalize(Decimal a) const {
00074    if (a >= _module || a < 0.0 ){
00075     a=fmod(a,_module);            //in [-2*_halfModule,2*_halfModule]
00076     if (a<0.0)      a+=_module;   //in [0,2*_halfModule]
00077     if (a>=_module) a-=_module;
00078    }
00079    return a;
00080   }
00082   Decimal zeroCentreNormalize(Decimal a) const {
00083    if (a > _halfModule || a <= -_halfModule ){
00084     a=fmod(a,_module);         //in [-2*_halfModule,2*_halfModule]
00085     if (a<=-_halfModule) a+=_module;
00086     if (a>_halfModule)  a-=_module;
00087    }
00088    return a;
00089   }
00090  public:
00092 //   S1() : _value(0.0) { };
00094   S1(const Decimal& m,const Decimal& t) {
00095    assert(m>0);
00096    _module   = m;
00097    _halfModule = _module/2.0;
00098    _value   = normalize(t);
00099   };
00101   S1(const S1& s){
00102    _module   = s._module;
00103    _halfModule = _module/2.0;
00104    _value    = normalize(s._value);
00105   };
00107   S1& operator=(const S1& s) {
00108    if (this != &s){      // Not necessary in this case but it is useful to don't forget it
00109     _module   = s._module;
00110     _halfModule = _module/2.0;
00111     _value   = normalize(s._value);
00112    }
00113    return *this;
00114   }
00116   S1& operator+=(const S1& a) {
00117    _value = normalize(_value + a._value);
00118    return *this;
00119   }
00121   S1& operator-=(const S1& a) {
00122    _value = normalize(_value - a._value);
00123    return *this;
00124   }
00126   S1& operator*=(const S1& a) {
00127    _value = normalize(_value * a._value);
00128    return *this;
00129   }
00131   S1 operator+(const S1 &other) const {
00132    return S1(*this) += other;
00133   }
00135   S1 operator-(const S1 &other) const {
00136    return S1(*this) -= other;
00137   }
00139   S1 operator*(const S1 &other) const {
00140    return S1(*this) *= other;
00141   }
00143   bool operator==(const S1 &other) const {
00144    if( ((Decimal) other ) == _value) return true;
00145    else return false;
00146   }
00148   bool operator!=(const S1 &other) const {
00149    return !(*this == other);
00150   }
00152   operator Decimal() const {
00153    return _value;
00154   }
00160   Decimal alDiff(const S1 &other) const {
00161    return zeroCentreNormalize(other._value - _value);
00162   }
00163   
00164   string print(int precision=2) const {
00165    stringstream s;
00166    s.precision(precision);
00167    s.setf(ios::fixed,ios::floatfield);
00168    s << _value;
00169    return s.str();
00170   }
00171 };
00172 
00176 class Angle{
00177  private:
00178 #ifdef FOUND_BOOST_SER
00179   friend class boost::serialization::access;
00180   template<class Archive>
00181   void serialize(Archive & ar, const unsigned int version){
00182    ar & theta; 
00183   }
00184 #endif
00185   
00186   Decimal theta;
00187   static const Decimal M_2PI; /*for efficency*/
00188   
00190   Decimal norm2Pi(Decimal a) const{
00191    if (a >= M_2PI || a < 0.0 ){
00192     a=fmod(a,M_2PI);            //in [-2*M_PI,2*M_PI]
00193     if (a<0.0)      a+=M_2PI;   //in [0,2*M_PI]
00194     if (a>=M_2PI) a-=M_2PI;
00195    }
00196    return a;
00197   }
00199   Decimal normPi(Decimal a,int opt=0) const{
00200    if (a > M_PI || a <= -M_PI ){
00201     a=fmod(a,M_2PI);         //in [-2*M_PI,2*M_PI]
00202     
00203                                 if(opt==0) {if (a<=-M_PI) a+=M_2PI;}
00204                                 if(opt==1) {if (a<-M_PI) a+=M_2PI;}
00205     if (a>M_PI)  a-=M_2PI;
00206    }
00207    return a;
00208   }
00210   Decimal normMinus2Pi(Decimal a) const {
00211    if (a >=0.0  || a < -M_2PI ){
00212     a=fmod(a,M_2PI);            //in [-2*M_PI,2*M_PI]
00213     if (a<-M_2PI) a+=M_2PI;   //in [0,2*M_PI]
00214     if (a>=0.0)   a-=M_2PI;
00215    }
00216    return a;
00217   }
00218  public:
00220   Angle() {
00221    theta = 0.0;
00222   };
00224   Angle(const Decimal& t) {
00225    theta = norm2Pi(t);
00226   };
00228   Angle(const Angle& rhs) {
00229    theta = norm2Pi(rhs.dCast2Pi());
00230   };
00232   Angle& operator=(const Angle& rhs){
00233    if (this != &rhs){      // Not necessary in this case but it is useful to don't forget it
00234     theta = norm2Pi(rhs.dCast2Pi());
00235    }
00236    return *this;
00237   }
00239   Angle& operator+=(const Angle& a) {
00240    theta = norm2Pi(theta + a.theta);
00241    return *this;
00242   }
00244   Angle& operator-=(const Angle& other) {
00245    Decimal myTheta = theta >= other.theta ? theta : theta + 2.0*M_PI;
00246    theta = norm2Pi(myTheta - other.theta);
00247    return *this;
00248   }
00250   Angle& operator*=(const Angle& a) {
00251    theta = norm2Pi(theta * a.theta);
00252    return *this;
00253   }
00255   Angle& operator*=(const Decimal& d) {
00256    theta = norm2Pi(theta * d);
00257    return *this;
00258   }
00260   Angle operator+(const Angle &other) const {
00261    return Angle(*this) += other;
00262   }
00264   Angle operator-(const Angle &other) const {
00265    return Angle(*this) -= other;
00266   }
00268   Angle operator*(const Angle &other) const {
00269    return Angle(*this) *= other;
00270   }
00272   Angle operator*(const Decimal& d) const {
00273    return (Angle(*this) *= d);
00274   }
00276   bool operator==(const Angle &other) const {
00277    if( (other.dCast2Pi()) == theta) return true;
00278    else return false;
00279   }
00281   bool operator!=(const Angle &other) const {
00282    return !(*this == other);
00283   }
00284   
00286   Decimal dCast() const {
00287    return norm2Pi(theta);
00288   }
00289   
00291   Decimal dCast2Pi() const {
00292    return norm2Pi(theta);
00293   }
00294   
00296   Decimal dCastPi(int opt=0) const {
00297    if(opt==0) return normPi(theta);
00298    if(opt==1) return normPi(theta,1);
00299   }
00301   Decimal dCastDeg() const {
00302    return (theta*180.0)/M_PI;
00303   }
00304   
00306   UCoordmm dCastHDeg() const {
00307    return (UCoordmm)((theta*18000.0)/M_PI);
00308   }
00313   Decimal alDiff(const Angle &other) const;
00314   
00319   Decimal ccwDiff(const Angle &other) const;
00320   
00325   Decimal cwDiff(const Angle &other) const;
00326   
00331   bool almostEqual(const Angle& A, const Decimal& toll) const;
00332   
00338   Angle nearestMean(const Angle &other, Decimal w1 = 0.5) const;
00339   
00344   Angle ccwMean(const Angle &other) const;
00345   
00350   Angle cwMean(const Angle &other) const;
00351   
00355   string print(int precision=2) const;
00356 };
00357 
00358 
00359 
00360 #endif
00361 
00362 
00363 
00364 /* @} */

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