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#include "TMath.h"
24
26
27
28
29namespace ROOT {
30
31namespace Math {
32
33
34//__________________________________________________________________________________________
35 /**
36 Class describing a polar 2D coordinate system based on r and phi
37 Phi is restricted to be in the range [-PI,PI)
38
39 @ingroup GenVector
40
41 @sa Overview of the @ref GenVector "physics vector library"
42 */
43
44
45template <class T>
46class Polar2D {
47
48public :
49
50 typedef T Scalar;
51 static constexpr unsigned int Dimension = 2U;
52
53 /**
54 Default constructor with r=1,phi=0
55 */
56 Polar2D() : fR(1.), fPhi(0) { }
57
58 /**
59 Construct from the polar coordinates: r and phi
60 */
61 Polar2D(T r,T phi) : fR(r), fPhi(phi) { Restrict(); }
62
63 /**
64 Construct from any Vector or coordinate system implementing
65 R() and Phi()
66 */
67 template <class CoordSystem >
68 explicit constexpr Polar2D( const CoordSystem & v ) :
69 fR(v.R() ), fPhi(v.Phi() ) { Restrict(); }
70
71 // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
72 // re-implement them ( there is no no need to have them with g++4)
73
74 /**
75 copy constructor
76 */
77 Polar2D(const Polar2D & v) :
78 fR(v.R() ), fPhi(v.Phi() ) { }
79
80 /**
81 assignment operator
82 */
84 fR = v.R();
85 fPhi = v.Phi();
86 return *this;
87 }
88
89
90 /**
91 Set internal data based on 2 Scalar numbers
92 */
94 { fR=r; fPhi=phi; Restrict(); }
95
96 /**
97 get internal data into 2 Scalar numbers
98 */
99 void GetCoordinates(Scalar& r, Scalar& phi) const {r=fR; phi=fPhi;}
100
101
102 Scalar R() const { return fR;}
103 Scalar Phi() const { return fPhi; }
104 Scalar X() const { using std::cos; return fR * cos(fPhi); }
105 Scalar Y() const { using std::sin; return fR * sin(fPhi); }
106 Scalar Mag2() const { return fR*fR;}
107
108
109 // setters (only for data members)
110
111
112 /**
113 set the r coordinate value keeping phi constant
114 */
115 void SetR(const T & r) {
116 fR = r;
117 }
118
119
120 /**
121 set the phi coordinate value keeping r constant
122 */
123 void SetPhi(const T & phi) {
124 fPhi = phi;
125 Restrict();
126 }
127
128 /**
129 set all values using cartesian coordinates
130 */
131 void SetXY(Scalar a, Scalar b);
132
133
134private:
135 inline static double pi() { return TMath::Pi(); }
136
137 /**
138 restrict abgle hi to be between -PI and PI
139 */
140 inline void Restrict() {
141 using std::floor;
142 if (fPhi <= -pi() || fPhi > pi()) fPhi = fPhi - floor(fPhi / (2 * pi()) + .5) * 2 * pi();
143 }
144
145public:
146
147 /**
148 scale by a scalar quantity - for polar coordinates r changes
149 */
150 void Scale (T a) {
151 if (a < 0) {
152 Negate();
153 a = -a;
154 }
155 // angles do not change when scaling by a positive quantity
156 fR *= a;
157 }
158
159 /**
160 negate the vector
161 */
162 void Negate ( ) {
163 fPhi = ( fPhi > 0 ? fPhi - pi() : fPhi + pi() );
164 }
165
166 /**
167 rotate the vector
168 */
169 void Rotate(T angle) {
170 fPhi += angle;
171 Restrict();
172 }
173
174 // assignment operators
175 /**
176 generic assignment operator from any coordinate system
177 */
178 template <class CoordSystem >
179 Polar2D & operator= ( const CoordSystem & c ) {
180 fR = c.R();
181 fPhi = c.Phi();
182 return *this;
183 }
184
185 /**
186 Exact equality
187 */
188 bool operator==(const Polar2D & rhs) const {
189 return fR == rhs.fR && fPhi == rhs.fPhi;
190 }
191 bool operator!= (const Polar2D & rhs) const {return !(operator==(rhs));}
192
193
194 // ============= Compatibility section ==================
195
196 // The following make this coordinate system look enough like a CLHEP
197 // vector that an assignment member template can work with either
198 T x() const { return X();}
199 T y() const { return Y();}
200
201 // ============= Specializations for improved speed ==================
202
203 // (none)
204
205#if defined(__MAKECINT__) || defined(G__DICTIONARY)
206
207 // ====== Set member functions for coordinates in other systems =======
208
209 void SetX(Scalar a);
210
211 void SetY(Scalar a);
212
213#endif
214
215private:
216 T fR;
218};
219
220
221 } // end namespace Math
222
223} // end namespace ROOT
224
225
226// move implementations here to avoid circle dependencies
227
229
230#if defined(__MAKECINT__) || defined(G__DICTIONARY)
232#endif
233
234namespace ROOT {
235
236 namespace Math {
237
238template <class T>
242
243
244#if defined(__MAKECINT__) || defined(G__DICTIONARY)
245
246
247// ====== Set member functions for coordinates in other systems =======
248
249 template <class T>
251 GenVector_exception e("Polar2D::SetX() is not supposed to be called");
252 throw e;
253 Cartesian2D<Scalar> v(*this); v.SetX(a); *this = Polar2D<Scalar>(v);
254 }
255 template <class T>
256 void Polar2D<T>::SetY(Scalar a) {
257 GenVector_exception e("Polar2D::SetY() is not supposed to be called");
258 throw e;
259 Cartesian2D<Scalar> v(*this); v.SetY(a); *this = Polar2D<Scalar>(v);
260 }
261
262#endif
263
264
265 } // end namespace Math
266
267} // end namespace ROOT
268
269
270
271#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
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition Polar2D.h:46
void Negate()
negate the vector
Definition Polar2D.h:162
void SetR(const T &r)
set the r coordinate value keeping phi constant
Definition Polar2D.h:115
Polar2D()
Default constructor with r=1,phi=0.
Definition Polar2D.h:56
void SetXY(Scalar a, Scalar b)
set all values using cartesian coordinates
Definition Polar2D.h:239
void SetCoordinates(Scalar r, Scalar phi)
Set internal data based on 2 Scalar numbers.
Definition Polar2D.h:93
void Scale(T a)
scale by a scalar quantity - for polar coordinates r changes
Definition Polar2D.h:150
Scalar X() const
Definition Polar2D.h:104
Polar2D(const Polar2D &v)
copy constructor
Definition Polar2D.h:77
Polar2D & operator=(const Polar2D &v)
assignment operator
Definition Polar2D.h:83
bool operator==(const Polar2D &rhs) const
Exact equality.
Definition Polar2D.h:188
constexpr Polar2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing R() and Phi()
Definition Polar2D.h:68
static double pi()
Definition Polar2D.h:135
Polar2D(T r, T phi)
Construct from the polar coordinates: r and phi.
Definition Polar2D.h:61
void Rotate(T angle)
rotate the vector
Definition Polar2D.h:169
void SetPhi(const T &phi)
set the phi coordinate value keeping r constant
Definition Polar2D.h:123
void GetCoordinates(Scalar &r, Scalar &phi) const
get internal data into 2 Scalar numbers
Definition Polar2D.h:99
bool operator!=(const Polar2D &rhs) const
Definition Polar2D.h:191
Scalar Phi() const
Definition Polar2D.h:103
Scalar Y() const
Definition Polar2D.h:105
static constexpr unsigned int Dimension
Definition Polar2D.h:51
Scalar Mag2() const
Definition Polar2D.h:106
void Restrict()
restrict abgle hi to be between -PI and PI
Definition Polar2D.h:140
Scalar R() const
Definition Polar2D.h:102
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...
constexpr Double_t Pi()
Definition TMath.h:38