CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DataFormats/GeometryVector/interface/Phi.h

Go to the documentation of this file.
00001 #ifndef GeometryVector_Geom_Phi_h
00002 #define GeometryVector_Geom_Phi_h
00003 
00004 #include "DataFormats/GeometryVector/interface/Pi.h"
00005 #include <cmath>
00006 
00007 namespace Geom {
00008 
00019   template <class T>
00020   class Phi {
00021   public:
00022 
00024     Phi() {}
00025 
00029     explicit Phi( const T& val) : theValue(val) { normalize();}
00030 
00032     operator T() const { return theValue;}
00033 
00035     template <class T1> operator Phi<T1>() { return Phi<T1>(theValue);}
00036 
00038     T value() const { return theValue;}
00039 
00041     Phi& operator+=(const T& a) {theValue+=a; normalize(); return *this;}
00042     Phi& operator+=(const Phi& a) {return operator+=(a.value());}
00043 
00044     Phi& operator-=(const T& a) {theValue-=a; normalize(); return *this;}
00045     Phi& operator-=(const Phi& a) {return operator-=(a.value());}
00046 
00047     Phi& operator*=(const T& a) {theValue*=a; normalize(); return *this;}
00048 
00049     Phi& operator/=(const T& a) {theValue/=a; normalize(); return *this;}
00050 
00051     T degrees() const { return theValue*180./pi();}
00052 
00053   private:
00054 
00055     T theValue;
00056 
00057     void normalize() { 
00058       if( theValue > twoPi() || theValue < -twoPi()) {
00059         theValue = fmod( theValue, (T) twoPi());
00060       }
00061       if (theValue <= -pi()) theValue += twoPi();
00062       if (theValue >  pi()) theValue -= twoPi();
00063     }
00064 
00065   };
00066 
00068   template <class T>
00069   inline Phi<T> operator-(const Phi<T>& a) {return Phi<T>(-a.value());}
00070 
00071 
00073   template <class T>
00074   inline Phi<T> operator+(const Phi<T>& a, const Phi<T>& b) {
00075     return Phi<T>(a) += b;
00076   }
00078   template <class T, class Scalar>
00079   inline Phi<T> operator+(const Phi<T>& a, const Scalar& b) {
00080     return Phi<T>(a) += b;
00081   }
00083   template <class T, class Scalar>
00084   inline Phi<T> operator+(const Scalar& a, const Phi<T>& b) {
00085     return Phi<T>(b) += a;
00086   }
00087 
00088 
00090   template <class T>
00091   inline Phi<T> operator-(const Phi<T>& a, const Phi<T>& b) { 
00092     return Phi<T>(a) -= b;
00093   }
00095   template <class T, class Scalar>
00096   inline Phi<T> operator-(const Phi<T>& a, const Scalar& b) { 
00097     return Phi<T>(a) -= b;
00098   }
00100   template <class T, class Scalar>
00101   inline Phi<T> operator-(const Scalar& a, const Phi<T>& b) { 
00102     return Phi<T>(a - b.value());  // use of unary operators would normalize twice
00103   }
00104 
00105 
00107   template <class T, class Scalar>
00108   inline Phi<T> operator*(const Phi<T>& a, const Scalar& b) {
00109     return Phi<T>(a) *= b;
00110   }
00112   template <class T>
00113   inline Phi<T> operator*(double a, const Phi<T>& b) {
00114     return Phi<T>(b) *= a;
00115   }
00116 
00117 
00119   template <class T>
00120   inline T operator/(const Phi<T>& a, const Phi<T>& b) { 
00121     return a.value() / b.value();
00122   }
00124   template <class T>
00125   inline Phi<T> operator/(const Phi<T>& a, double b) {
00126     return Phi<T>(a) /= b;
00127   }
00128 
00129 
00130 }
00131 #endif
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 
00141 
00142