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