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