Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Cartesian2D.h
Go to the documentation of this file.
1// @(#)root/mathcore:$Id: b12794c790afad19142e34a401af6c233aba446b $
2// Authors: W. Brown, M. Fischler, L. Moneta 2005
3
4 /**********************************************************************
5 * *
6 * Copyright (c) 2005 , LCG ROOT MathLib Team *
7 * & FNAL LCG ROOT Mathlib Team *
8 * *
9 * *
10 **********************************************************************/
11
12// Header file for class Cartesian2D
13//
14// Created by: Lorenzo Moneta at Mon 16 Apr 2007
15//
16#ifndef ROOT_Math_GenVector_Cartesian2D
17#define ROOT_Math_GenVector_Cartesian2D 1
18
20
21#include "Math/Math.h"
22
23
24namespace ROOT {
25
26namespace Math {
27
28//__________________________________________________________________________________________
29 /**
30 Class describing a 2D cartesian coordinate system
31 (x, y coordinates)
32
33 @ingroup GenVector
34 */
35
36template <class T = double>
38
39public :
40
41 typedef T Scalar;
42
43 /**
44 Default constructor with x=y=0
45 */
46 Cartesian2D() : fX(0.0), fY(0.0) { }
47
48 /**
49 Constructor from x,y coordinates
50 */
51 Cartesian2D(Scalar xx, Scalar yy) : fX(xx), fY(yy) { }
52
53 /**
54 Construct from any Vector or coordinate system implementing
55 X() and Y()
56 */
57 template <class CoordSystem>
58 explicit Cartesian2D(const CoordSystem & v)
59 : fX(v.X()), fY(v.Y()) { }
60
61
62 // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
63 // re-implement them ( there is no no need to have them with g++4)
64 /**
65 copy constructor
66 */
68 fX(v.X()), fY(v.Y()) { }
69
70 /**
71 assignment operator
72 */
74 fX = v.X();
75 fY = v.Y();
76 return *this;
77 }
78
79 /**
80 Set internal data based on 2 Scalar numbers
81 */
82 void SetCoordinates(Scalar xx, Scalar yy) { fX=xx; fY=yy; }
83
84 /**
85 get internal data into 2 Scalar numbers
86 */
87 void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }
88
89 Scalar X() const { return fX;}
90 Scalar Y() const { return fY;}
91 Scalar Mag2() const { return fX*fX + fY*fY; }
92 Scalar R() const { using std::sqrt; return sqrt(Mag2()); }
93 Scalar Phi() const { using std::atan2; return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : atan2(fY, fX); }
94
95 /**
96 set the x coordinate value keeping y constant
97 */
98 void SetX(Scalar a) { fX = a; }
99
100 /**
101 set the y coordinate value keeping x constant
102 */
103 void SetY(Scalar a) { fY = a; }
104
105 /**
106 set all values using cartesian coordinates
107 */
108 void SetXY(Scalar xx, Scalar yy ) {
109 fX=xx;
110 fY=yy;
111 }
112
113 /**
114 scale the vector by a scalar quantity a
115 */
116 void Scale(Scalar a) { fX *= a; fY *= a; }
117
118 /**
119 negate the vector
120 */
121 void Negate() { fX = -fX; fY = -fY; }
122
123 /**
124 rotate by an angle
125 */
126 void Rotate(Scalar angle) {
127 using std::sin;
128 const Scalar s = sin(angle);
129 using std::cos;
130 const Scalar c = cos(angle);
131 SetCoordinates(c * fX - s * fY, s * fX + c * fY);
132 }
133
134 /**
135 Assignment from any class implementing x(),y()
136 (can assign from any coordinate system)
137 */
138 template <class CoordSystem>
139 Cartesian2D & operator = (const CoordSystem & v) {
140 fX = v.x();
141 fY = v.y();
142 return *this;
143 }
144
145 /**
146 Exact equality
147 */
148 bool operator == (const Cartesian2D & rhs) const {
149 return fX == rhs.fX && fY == rhs.fY;
150 }
151 bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
152
153
154 // ============= Compatibility section ==================
155
156 // The following make this coordinate system look enough like a CLHEP
157 // vector that an assignment member template can work with either
158 Scalar x() const { return X();}
159 Scalar y() const { return Y();}
160
161 // ============= Overloads for improved speed ==================
162
163 template <class T2>
164 explicit Cartesian2D( const Polar2D<T2> & v )
165 {
166 const Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
167 // is the speed improvement
168 using std::cos;
169 fX = r * cos(v.Phi());
170 using std::sin;
171 fY = r * sin(v.Phi());
172 }
173 // Technical note: This works even though only Polar2Dfwd.h is
174 // included (and in fact, including Polar2D.h would cause circularity
175 // problems). It works because any program **using** this ctor must itself
176 // be including Polar2D.h.
177
178 template <class T2>
180 {
181 const Scalar r = v.R();
182 using std::cos;
183 fX = r * cos(v.Phi());
184 using std::sin;
185 fY = r * sin(v.Phi());
186 return *this;
187 }
188
189
190
191#if defined(__MAKECINT__) || defined(G__DICTIONARY)
192
193 // ====== Set member functions for coordinates in other systems =======
194
195 void SetR(Scalar r);
196
197 void SetPhi(Scalar phi);
198
199#endif
200
201
202private:
203
204 /**
205 (Contiguous) data containing the coordinates values x and y
206 */
207 T fX;
208 T fY;
209
210};
211
212
213 } // end namespace Math
214
215} // end namespace ROOT
216
217
218#if defined(__MAKECINT__) || defined(G__DICTIONARY)
219// need to put here setter methods to resolve nasty cyclical dependencies
220// I need to include other coordinate systems only when Cartesian is already defined
221// since they depend on it
222
225
226// ====== Set member functions for coordinates in other systems =======
227
228namespace ROOT {
229
230 namespace Math {
231
232 template <class T>
233 void Cartesian2D<T>::SetR(Scalar r) {
234 GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
235 throw e;
236 Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
237 }
238
239
240 template <class T>
241 void Cartesian2D<T>::SetPhi(Scalar phi) {
242 GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
243 throw e;
244 Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
245 }
246
247
248
249 } // end namespace Math
250
251} // end namespace ROOT
252
253#endif
254
255
256
257
258#endif /* ROOT_Math_GenVector_Cartesian2D */
ROOT::R::TRInterface & r
Definition Object.C:4
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition Cartesian2D.h:37
Cartesian2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X() and Y()
Definition Cartesian2D.h:58
void SetX(Scalar a)
set the x coordinate value keeping y constant
Definition Cartesian2D.h:98
bool operator!=(const Cartesian2D &rhs) const
bool operator==(const Cartesian2D &rhs) const
Exact equality.
void Scale(Scalar a)
scale the vector by a scalar quantity a
void Negate()
negate the vector
void SetY(Scalar a)
set the y coordinate value keeping x constant
void SetXY(Scalar xx, Scalar yy)
set all values using cartesian coordinates
Cartesian2D(const Cartesian2D &v)
copy constructor
Definition Cartesian2D.h:67
Cartesian2D()
Default constructor with x=y=0.
Definition Cartesian2D.h:46
Cartesian2D(const Polar2D< T2 > &v)
Cartesian2D & operator=(const Cartesian2D &v)
assignment operator
Definition Cartesian2D.h:73
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition Cartesian2D.h:87
Scalar Mag2() const
Definition Cartesian2D.h:91
void SetCoordinates(Scalar xx, Scalar yy)
Set internal data based on 2 Scalar numbers.
Definition Cartesian2D.h:82
T fX
(Contiguous) data containing the coordinates values x and y
void Rotate(Scalar angle)
rotate by an angle
Cartesian2D(Scalar xx, Scalar yy)
Constructor from x,y coordinates.
Definition Cartesian2D.h:51
Class describing a polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition Polar2D.h:43
Namespace for new Math classes and functions.
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
Rotation3D::Scalar Scalar
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...