Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Polar2D.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id$
2// Authors: W. Brown, M. Fischler, L. Moneta 2005
3
4 /**********************************************************************
5 * *
6 * Copyright (c) 2005 , LCG ROOT MathLib Team and *
7 * FNAL LCG ROOT MathLib Team *
8 * *
9 * *
10 **********************************************************************/
11
12// Header file for class Polar2D
13//
14// Created by: Lorenzo Moneta at Mon May 30 11:40:03 2005
15// Major revamp: M. Fischler at Wed Jun 8 2005
16//
17// Last update: $Id$
18//
19#ifndef ROOT_Math_GenVector_Polar2D
20#define ROOT_Math_GenVector_Polar2D 1
21
22#include "Math/Math.h"
23
25
26
27
28namespace ROOT {
29
30namespace Math {
31
32
33//__________________________________________________________________________________________
34 /**
35 Class describing a polar 2D coordinate system based on r and phi
36 Phi is restricted to be in the range [-PI,PI)
37
38 @ingroup GenVector
39
40 @sa Overview of the @ref GenVector "physics vector library"
41 */
42
43
44template <class T>
45class Polar2D {
46
47public :
48
49 typedef T Scalar;
50 static constexpr unsigned int Dimension = 2U;
51
52 /**
53 Default constructor with r=1,phi=0
54 */
55 Polar2D() : fR(1.), fPhi(0) { }
56
57 /**
58 Construct from the polar coordinates: r and phi
59 */
60 Polar2D(T r,T phi) : fR(r), fPhi(phi) { Restrict(); }
61
62 /**
63 Construct from any Vector or coordinate system implementing
64 R() and Phi()
65 */
66 template <class CoordSystem >
67 explicit constexpr Polar2D( const CoordSystem & v ) :
68 fR(v.R() ), fPhi(v.Phi() ) { Restrict(); }
69
70 // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
71 // re-implement them ( there is no no need to have them with g++4)
72
73 /**
74 copy constructor
75 */
76 Polar2D(const Polar2D & v) :
77 fR(v.R() ), fPhi(v.Phi() ) { }
78
79 /**
80 assignment operator
81 */
83 fR = v.R();
84 fPhi = v.Phi();
85 return *this;
86 }
87
88
89 /**
90 Set internal data based on 2 Scalar numbers
91 */
93 { fR=r; fPhi=phi; Restrict(); }
94
95 /**
96 get internal data into 2 Scalar numbers
97 */
98 void GetCoordinates(Scalar& r, Scalar& phi) const {r=fR; phi=fPhi;}
99
100
101 Scalar R() const { return fR;}
102 Scalar Phi() const { return fPhi; }
103 Scalar X() const { using std::cos; return fR * cos(fPhi); }
104 Scalar Y() const { using std::sin; return fR * sin(fPhi); }
105 Scalar Mag2() const { return fR*fR;}
106
107
108 // setters (only for data members)
109
110
111 /**
112 set the r coordinate value keeping phi constant
113 */
114 void SetR(const T & r) {
115 fR = r;
116 }
117
118
119 /**
120 set the phi coordinate value keeping r constant
121 */
122 void SetPhi(const T & phi) {
123 fPhi = phi;
124 Restrict();
125 }
126
127 /**
128 set all values using cartesian coordinates
129 */
130 void SetXY(Scalar a, Scalar b);
131
132
133private:
134 inline static double pi() { return M_PI; }
135
136 /**
137 restrict abgle hi to be between -PI and PI
138 */
139 inline void Restrict() {
140 using std::floor;
141 if (fPhi <= -pi() || fPhi > pi()) fPhi = fPhi - floor(fPhi / (2 * pi()) + .5) * 2 * pi();
142 }
143
144public:
145
146 /**
147 scale by a scalar quantity - for polar coordinates r changes
148 */
149 void Scale (T a) {
150 if (a < 0) {
151 Negate();
152 a = -a;
153 }
154 // angles do not change when scaling by a positive quantity
155 fR *= a;
156 }
157
158 /**
159 negate the vector
160 */
161 void Negate ( ) {
162 fPhi = ( fPhi > 0 ? fPhi - pi() : fPhi + pi() );
163 }
164
165 /**
166 rotate the vector
167 */
168 void Rotate(T angle) {
169 fPhi += angle;
170 Restrict();
171 }
172
173 // assignment operators
174 /**
175 generic assignment operator from any coordinate system
176 */
177 template <class CoordSystem >
178 Polar2D & operator= ( const CoordSystem & c ) {
179 fR = c.R();
180 fPhi = c.Phi();
181 return *this;
182 }
183
184 /**
185 Exact equality
186 */
187 bool operator==(const Polar2D & rhs) const {
188 return fR == rhs.fR && fPhi == rhs.fPhi;
189 }
190 bool operator!= (const Polar2D & rhs) const {return !(operator==(rhs));}
191
192
193 // ============= Compatibility section ==================
194
195 // The following make this coordinate system look enough like a CLHEP
196 // vector that an assignment member template can work with either
197 T x() const { return X();}
198 T y() const { return Y();}
199
200 // ============= Specializations for improved speed ==================
201
202 // (none)
203
204#if defined(__MAKECINT__) || defined(G__DICTIONARY)
205
206 // ====== Set member functions for coordinates in other systems =======
207
208 void SetX(Scalar a);
209
210 void SetY(Scalar a);
211
212#endif
213
214private:
215 T fR;
217};
218
219
220 } // end namespace Math
221
222} // end namespace ROOT
223
224
225// move implementations here to avoid circle dependencies
226
228
229#if defined(__MAKECINT__) || defined(G__DICTIONARY)
231#endif
232
233namespace ROOT {
234
235 namespace Math {
236
237template <class T>
239 *this = Cartesian2D<Scalar>(a, b);
240}
241
242
243#if defined(__MAKECINT__) || defined(G__DICTIONARY)
244
245
246// ====== Set member functions for coordinates in other systems =======
247
248 template <class T>
250 GenVector_exception e("Polar2D::SetX() is not supposed to be called");
251 throw e;
252 Cartesian2D<Scalar> v(*this); v.SetX(a); *this = Polar2D<Scalar>(v);
253 }
254 template <class T>
255 void Polar2D<T>::SetY(Scalar a) {
256 GenVector_exception e("Polar2D::SetY() is not supposed to be called");
257 throw e;
258 Cartesian2D<Scalar> v(*this); v.SetY(a); *this = Polar2D<Scalar>(v);
259 }
260
261#endif
262
263
264 } // end namespace Math
265
266} // end namespace ROOT
267
268
269
270#endif /* ROOT_Math_GenVector_Polar2D */
#define b(i)
Definition RSha256.hxx:100
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
#define M_PI
Definition Rotated.cxx:105
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint angle
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition Cartesian2D.h:39
Class describing a polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition Polar2D.h:45
void Negate()
negate the vector
Definition Polar2D.h:161
void SetR(const T &r)
set the r coordinate value keeping phi constant
Definition Polar2D.h:114
Polar2D()
Default constructor with r=1,phi=0.
Definition Polar2D.h:55
void SetXY(Scalar a, Scalar b)
set all values using cartesian coordinates
Definition Polar2D.h:238
void SetCoordinates(Scalar r, Scalar phi)
Set internal data based on 2 Scalar numbers.
Definition Polar2D.h:92
void Scale(T a)
scale by a scalar quantity - for polar coordinates r changes
Definition Polar2D.h:149
Scalar X() const
Definition Polar2D.h:103
Polar2D(const Polar2D &v)
copy constructor
Definition Polar2D.h:76
Polar2D & operator=(const Polar2D &v)
assignment operator
Definition Polar2D.h:82
bool operator==(const Polar2D &rhs) const
Exact equality.
Definition Polar2D.h:187
constexpr Polar2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing R() and Phi()
Definition Polar2D.h:67
static double pi()
Definition Polar2D.h:134
Polar2D(T r, T phi)
Construct from the polar coordinates: r and phi.
Definition Polar2D.h:60
void Rotate(T angle)
rotate the vector
Definition Polar2D.h:168
void SetPhi(const T &phi)
set the phi coordinate value keeping r constant
Definition Polar2D.h:122
void GetCoordinates(Scalar &r, Scalar &phi) const
get internal data into 2 Scalar numbers
Definition Polar2D.h:98
bool operator!=(const Polar2D &rhs) const
Definition Polar2D.h:190
Scalar Phi() const
Definition Polar2D.h:102
Scalar Y() const
Definition Polar2D.h:104
static constexpr unsigned int Dimension
Definition Polar2D.h:50
Scalar Mag2() const
Definition Polar2D.h:105
void Restrict()
restrict abgle hi to be between -PI and PI
Definition Polar2D.h:139
Scalar R() const
Definition Polar2D.h:101
Namespace for new Math classes and functions.
Rotation3D::Scalar Scalar
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...