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 @see GenVector
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 constexpr Polar2D() noexcept = default;
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 /**
72 Set internal data based on 2 Scalar numbers
73 */
75 { fR=r; fPhi=phi; Restrict(); }
76
77 /**
78 get internal data into 2 Scalar numbers
79 */
80 void GetCoordinates(Scalar& r, Scalar& phi) const {r=fR; phi=fPhi;}
81
82
83 Scalar R() const { return fR;}
84 Scalar Phi() const { return fPhi; }
85 Scalar X() const { using std::cos; return fR * cos(fPhi); }
86 Scalar Y() const { using std::sin; return fR * sin(fPhi); }
87 Scalar Mag2() const { return fR*fR;}
88
89
90 // setters (only for data members)
91
92
93 /**
94 set the r coordinate value keeping phi constant
95 */
96 void SetR(const T & r) {
97 fR = r;
98 }
99
100
101 /**
102 set the phi coordinate value keeping r constant
103 */
104 void SetPhi(const T & phi) {
105 fPhi = phi;
106 Restrict();
107 }
108
109 /**
110 set all values using cartesian coordinates
111 */
112 void SetXY(Scalar a, Scalar b);
113
114
115private:
116 inline static double pi() { return TMath::Pi(); }
117
118 /**
119 restrict abgle hi to be between -PI and PI
120 */
121 inline void Restrict() {
122 using std::floor;
123 if (fPhi <= -pi() || fPhi > pi()) fPhi = fPhi - floor(fPhi / (2 * pi()) + .5) * 2 * pi();
124 }
125
126public:
127
128 /**
129 scale by a scalar quantity - for polar coordinates r changes
130 */
131 void Scale (T a) {
132 if (a < 0) {
133 Negate();
134 a = -a;
135 }
136 // angles do not change when scaling by a positive quantity
137 fR *= a;
138 }
139
140 /**
141 negate the vector
142 */
143 void Negate ( ) {
144 fPhi = ( fPhi > 0 ? fPhi - pi() : fPhi + pi() );
145 }
146
147 /**
148 rotate the vector
149 */
150 void Rotate(T angle) {
151 fPhi += angle;
152 Restrict();
153 }
154
155 // assignment operators
156 /**
157 generic assignment operator from any coordinate system
158 */
159 template <class CoordSystem >
160 Polar2D & operator= ( const CoordSystem & c ) {
161 fR = c.R();
162 fPhi = c.Phi();
163 return *this;
164 }
165
166 /**
167 Exact equality
168 */
169 bool operator==(const Polar2D & rhs) const {
170 return fR == rhs.fR && fPhi == rhs.fPhi;
171 }
172 bool operator!= (const Polar2D & rhs) const {return !(operator==(rhs));}
173
174
175 // ============= Compatibility section ==================
176
177 // The following make this coordinate system look enough like a CLHEP
178 // vector that an assignment member template can work with either
179 T x() const { return X();}
180 T y() const { return Y();}
181
182 // ============= Specializations for improved speed ==================
183
184 // (none)
185
186#if defined(__MAKECINT__) || defined(G__DICTIONARY)
187
188 // ====== Set member functions for coordinates in other systems =======
189
190 void SetX(Scalar a);
191
192 void SetY(Scalar a);
193
194#endif
195
196private:
197 T fR = 1.;
198 T fPhi = 0.;
199};
200
201
202 } // end namespace Math
203
204} // end namespace ROOT
205
206
207// move implementations here to avoid circle dependencies
208
210
211#if defined(__MAKECINT__) || defined(G__DICTIONARY)
213#endif
214
215namespace ROOT {
216
217 namespace Math {
218
219template <class T>
223
224
225#if defined(__MAKECINT__) || defined(G__DICTIONARY)
226
227
228// ====== Set member functions for coordinates in other systems =======
229
230 template <class T>
232 GenVector_exception e("Polar2D::SetX() is not supposed to be called");
233 throw e;
234 Cartesian2D<Scalar> v(*this); v.SetX(a); *this = Polar2D<Scalar>(v);
235 }
236 template <class T>
237 void Polar2D<T>::SetY(Scalar a) {
238 GenVector_exception e("Polar2D::SetY() is not supposed to be called");
239 throw e;
240 Cartesian2D<Scalar> v(*this); v.SetY(a); *this = Polar2D<Scalar>(v);
241 }
242
243#endif
244
245
246 } // end namespace Math
247
248} // end namespace ROOT
249
250
251
252#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:143
void SetR(const T &r)
set the r coordinate value keeping phi constant
Definition Polar2D.h:96
void SetXY(Scalar a, Scalar b)
set all values using cartesian coordinates
Definition Polar2D.h:220
void SetCoordinates(Scalar r, Scalar phi)
Set internal data based on 2 Scalar numbers.
Definition Polar2D.h:74
Polar2D & operator=(const CoordSystem &c)
generic assignment operator from any coordinate system
Definition Polar2D.h:160
void Scale(T a)
scale by a scalar quantity - for polar coordinates r changes
Definition Polar2D.h:131
Scalar X() const
Definition Polar2D.h:85
constexpr Polar2D() noexcept=default
Default constructor with r=1,phi=0.
bool operator==(const Polar2D &rhs) const
Exact equality.
Definition Polar2D.h:169
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:116
void Rotate(T angle)
rotate the vector
Definition Polar2D.h:150
void SetPhi(const T &phi)
set the phi coordinate value keeping r constant
Definition Polar2D.h:104
void GetCoordinates(Scalar &r, Scalar &phi) const
get internal data into 2 Scalar numbers
Definition Polar2D.h:80
bool operator!=(const Polar2D &rhs) const
Definition Polar2D.h:172
Scalar Phi() const
Definition Polar2D.h:84
Scalar Y() const
Definition Polar2D.h:86
static constexpr unsigned int Dimension
Definition Polar2D.h:51
Scalar Mag2() const
Definition Polar2D.h:87
void Restrict()
restrict abgle hi to be between -PI and PI
Definition Polar2D.h:121
Scalar R() const
Definition Polar2D.h:83
Namespace for new Math classes and functions.
Rotation3D::Scalar Scalar
Namespace for new ROOT classes and functions.
constexpr Double_t Pi()
Definition TMath.h:38