CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Phi.h
Go to the documentation of this file.
1 #ifndef GeometryVector_Geom_Phi_h
2 #define GeometryVector_Geom_Phi_h
3 
5 #include <cmath>
6 
7 namespace Geom {
8 
19  template <class T>
20  class Phi {
21  public:
22 
24  Phi() {}
25 
29  explicit Phi( const T& val) : theValue(val) { normalize();}
30 
32  operator T() const { return theValue;}
33 
35  template <class T1> operator Phi<T1>() { return Phi<T1>(theValue);}
36 
38  T value() const { return theValue;}
39 
41  Phi& operator+=(const T& a) {theValue+=a; normalize(); return *this;}
42  Phi& operator+=(const Phi& a) {return operator+=(a.value());}
43 
44  Phi& operator-=(const T& a) {theValue-=a; normalize(); return *this;}
45  Phi& operator-=(const Phi& a) {return operator-=(a.value());}
46 
47  Phi& operator*=(const T& a) {theValue*=a; normalize(); return *this;}
48 
49  Phi& operator/=(const T& a) {theValue/=a; normalize(); return *this;}
50 
51  T degrees() const { return theValue*180./pi();}
52 
53  private:
54 
56 
57  void normalize() {
58  if( theValue > twoPi() || theValue < -twoPi()) {
59  theValue = fmod( theValue, (T) twoPi());
60  }
61  if (theValue <= -pi()) theValue += twoPi();
62  if (theValue > pi()) theValue -= twoPi();
63  }
64 
65  };
66 
68  template <class T>
69  inline Phi<T> operator-(const Phi<T>& a) {return Phi<T>(-a.value());}
70 
71 
73  template <class T>
74  inline Phi<T> operator+(const Phi<T>& a, const Phi<T>& b) {
75  return Phi<T>(a) += b;
76  }
78  template <class T, class Scalar>
79  inline Phi<T> operator+(const Phi<T>& a, const Scalar& b) {
80  return Phi<T>(a) += b;
81  }
83  template <class T, class Scalar>
84  inline Phi<T> operator+(const Scalar& a, const Phi<T>& b) {
85  return Phi<T>(b) += a;
86  }
87 
88 
90  template <class T>
91  inline Phi<T> operator-(const Phi<T>& a, const Phi<T>& b) {
92  return Phi<T>(a) -= b;
93  }
95  template <class T, class Scalar>
96  inline Phi<T> operator-(const Phi<T>& a, const Scalar& b) {
97  return Phi<T>(a) -= b;
98  }
100  template <class T, class Scalar>
101  inline Phi<T> operator-(const Scalar& a, const Phi<T>& b) {
102  return Phi<T>(a - b.value()); // use of unary operators would normalize twice
103  }
104 
105 
107  template <class T, class Scalar>
108  inline Phi<T> operator*(const Phi<T>& a, const Scalar& b) {
109  return Phi<T>(a) *= b;
110  }
112  template <class T>
113  inline Phi<T> operator*(double a, const Phi<T>& b) {
114  return Phi<T>(b) *= a;
115  }
116 
117 
119  template <class T>
120  inline T operator/(const Phi<T>& a, const Phi<T>& b) {
121  return a.value() / b.value();
122  }
124  template <class T>
125  inline Phi<T> operator/(const Phi<T>& a, double b) {
126  return Phi<T>(a) /= b;
127  }
128 
129 
130 }
131 #endif
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
T operator/(const OnePiRange< T > &a, const OnePiRange< T > &b)
Division.
Definition: OnePiRange.h:122
OnePiRange< T > operator-(const OnePiRange< T > &a)
Definition: OnePiRange.h:71
Phi & operator-=(const Phi &a)
Definition: Phi.h:45
void normalize()
Definition: Phi.h:57
Phi & operator-=(const T &a)
Definition: Phi.h:44
double Scalar
Definition: Definitions.h:27
Phi & operator/=(const T &a)
Definition: Phi.h:49
Phi()
Default constructor does not initialise - just as double.
Definition: Phi.h:24
Phi & operator+=(const Phi &a)
Definition: Phi.h:42
OnePiRange< T > operator+(const OnePiRange< T > &a, const OnePiRange< T > &b)
Addition.
Definition: OnePiRange.h:76
T theValue
Definition: Phi.h:55
Phi(const T &val)
Definition: Phi.h:29
OnePiRange< T > operator*(const OnePiRange< T > &a, const Scalar &b)
Multiplication with scalar, does not change the precision.
Definition: OnePiRange.h:110
T value() const
Explicit access to value in case implicit conversion not OK.
Definition: Phi.h:38
Phi & operator+=(const T &a)
Standard arithmetics.
Definition: Phi.h:41
double b
Definition: hdecay.h:120
double pi()
Definition: Pi.h:31
double a
Definition: hdecay.h:121
double twoPi()
Definition: Pi.h:32
Phi & operator*=(const T &a)
Definition: Phi.h:47
T degrees() const
Definition: Phi.h:51
long double T
Definition: Phi.h:20