StRoot  1
StAngle.hh
1 /***************************************************************************
2  *
3  * \$Id: StAngle.hh,v 1.1 2001/04/20 04:54:26 ullrich Exp \$
4  *
5  * Author: Thomas Ullrich, April 2001
6  ***************************************************************************
7  *
8  * Description:
9  * Keeps an angle within the range of [-pi, pi] and deals with differences
10  * between angles in the right way. Units are radian.
11  * Other than that it can be handled pretty much like a 'double'.
12  *
13  ***************************************************************************
14  *
15  * \$Log: StAngle.hh,v \$
16  * Revision 1.1 2001/04/20 04:54:26 ullrich
17  * Initial Revision.
18  *
19  **************************************************************************/
20 #ifndef StAngle_hh
21 #define StAngle_hh
22
23 #include <cmath>
24 #include <float.h>
25
26 class StAngle {
27 public:
28  StAngle();
29  StAngle(const StAngle& val);
30  StAngle(double);
31
32  double degree(); // range [0-360]
33
34  operator double() const { return phi; }
35
36  StAngle operator= (double);
37  StAngle operator+= (StAngle);
38  StAngle operator-= (StAngle);
39  StAngle operator*= (double);
40  StAngle operator/= (double);
41  int operator== (const StAngle&) const;
42
43  friend StAngle operator+ (StAngle, StAngle);
44  friend StAngle operator- (StAngle, StAngle);
45  friend StAngle operator* (StAngle, double);
46  friend StAngle operator/ (StAngle, double);
47
48  friend StAngle average(StAngle, StAngle);
49
50 private:
51  double phi;
52 };
53
54 //
55 // Constructors for angles
56 //
57
58 inline StAngle::StAngle()
59 {
60  phi = 0.; // default: set to 0
61 }
62
63 inline StAngle::StAngle(const StAngle& val)
64 {
65  phi = val.phi;
66 }
67
68 inline StAngle::StAngle(double val)
69 {
70  phi = val;
71  if (fabs(phi) > M_PI) phi = atan2(sin(phi), cos(phi));
72 }
73
74 //
75 // Here follow the member functions for the operators ==, !=, =, +=, -=, *=, /=
76 //
77
78 inline StAngle StAngle::operator= (double val)
79 {
80  phi = val;
81  if (fabs(phi) > M_PI) phi = atan2(sin(phi), cos(phi));
82  return *this;
83 }
84
85 inline StAngle StAngle::operator+= (StAngle val)
86 {
87  phi += val.phi;
88  if (fabs(phi) > M_PI) phi = atan2(sin(phi), cos(phi));
89  return *this;
90 }
91
92 inline StAngle StAngle::operator-= (StAngle val)
93 {
94  phi -= val.phi;
95  if (fabs(phi) > M_PI) phi = atan2(sin(phi), cos(phi));
96  return *this;
97 }
98
99 inline StAngle StAngle::operator*= (double val)
100 {
101  phi *= val;
102  if (fabs(phi) > M_PI) phi = atan2(sin(phi), cos(phi));
103  return *this;
104 }
105
106 inline StAngle StAngle::operator/= (double val)
107 {
108  phi /= val;
109  if (fabs(phi) > M_PI) phi = atan2(sin(phi), cos(phi));
110  return *this;
111 }
112
113 inline int StAngle::operator== (const StAngle& a) const
114 {
115  return (fabs(StAngle(phi-a.phi)) < FLT_EPSILON);
116 }
117
118 //
119 // The friends for operator +, -, *, /
120 //
121
122 inline StAngle operator+ (StAngle angle, StAngle val)
123 {
124  double res = angle.phi + val.phi;
125  if (fabs(res) > M_PI) res = atan2(sin(res), cos(res));
126  return res;
127 }
128
129 inline StAngle operator- (StAngle angle, StAngle val)
130 {
131  double res = angle.phi - val.phi;
132  if (fabs(res) > M_PI) res = atan2(sin(res), cos(res));
133  return res;
134 }
135
136 inline StAngle operator* (StAngle angle, double val)
137 {
138  double res = angle.phi * val;
139  if (fabs(res) > M_PI) res = atan2(sin(res), cos(res));
140  return res;
141 }
142
143 inline StAngle operator/ (StAngle angle, double val)
144 {
145  double res = angle.phi / val;
146  if (fabs(res) > M_PI) res = atan2(sin(res), cos(res));
147  return res;
148 }
149
150 inline StAngle average(StAngle a, StAngle b)
151 {
152  return a+(b-a)/2.;
153 }
154
155 //
156 // Return angle in degrees [0, 360]
157 //
158
159 inline double StAngle::degree()
160 {
161  double val = phi*180./M_PI;
162  return (val < 0) ? val += 360 : val;
163 }
164
165
166 #endif