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