ROOT   6.08/07 Reference Guide
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
19 #ifndef ROOT_Math_GenVector_Polar2Dfwd
21 #endif
22
23 #ifndef ROOT_Math_Math
24 #include "Math/Math.h"
25 #endif
26
27
28 namespace ROOT {
29
30 namespace Math {
31
32 //__________________________________________________________________________________________
33  /**
34  Class describing a 2D cartesian coordinate system
35  (x, y coordinates)
36
37  @ingroup GenVector
38  */
39
40 template <class T = double>
41 class Cartesian2D {
42
43 public :
44
45  typedef T Scalar;
46
47  /**
48  Default constructor with x=y=0
49  */
50  Cartesian2D() : fX(0.0), fY(0.0) { }
51
52  /**
53  Constructor from x,y coordinates
54  */
55  Cartesian2D(Scalar xx, Scalar yy) : fX(xx), fY(yy) { }
56
57  /**
58  Construct from any Vector or coordinate system implementing
59  X() and Y()
60  */
61  template <class CoordSystem>
62  explicit Cartesian2D(const CoordSystem & v)
63  : fX(v.X()), fY(v.Y()) { }
64
65
66  // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
67  // re-implement them ( there is no no need to have them with g++4)
68  /**
69  copy constructor
70  */
72  fX(v.X()), fY(v.Y()) { }
73
74  /**
75  assignment operator
76  */
78  fX = v.X();
79  fY = v.Y();
80  return *this;
81  }
82
83  /**
84  Set internal data based on 2 Scalar numbers
85  */
86  void SetCoordinates(Scalar xx, Scalar yy) { fX=xx; fY=yy; }
87
88  /**
89  get internal data into 2 Scalar numbers
90  */
91  void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }
92
93  Scalar X() const { return fX;}
94  Scalar Y() const { return fY;}
95  Scalar Mag2() const { return fX*fX + fY*fY; }
96  Scalar R() const { return std::sqrt( Mag2());}
97  Scalar Phi() const { return (fX==0 && fY==0) ? 0.0 : atan2(fY,fX);}
98
99  /**
100  set the x coordinate value keeping y constant
101  */
102  void SetX(Scalar a) { fX = a; }
103
104  /**
105  set the y coordinate value keeping x constant
106  */
107  void SetY(Scalar a) { fY = a; }
108
109  /**
110  set all values using cartesian coordinates
111  */
112  void SetXY(Scalar xx, Scalar yy ) {
113  fX=xx;
114  fY=yy;
115  }
116
117  /**
118  scale the vector by a scalar quantity a
119  */
120  void Scale(Scalar a) { fX *= a; fY *= a; }
121
122  /**
123  negate the vector
124  */
125  void Negate() { fX = -fX; fY = -fY; }
126
127  /**
128  rotate by an angle
129  */
130  void Rotate(Scalar angle) {
131  Scalar s = std::sin(angle);
132  Scalar c = std::cos(angle);
133  SetCoordinates( c*fX - s*fY, s*fX + c * fY );
134  }
135
136  /**
137  Assignment from any class implementing x(),y()
138  (can assign from any coordinate system)
139  */
140  template <class CoordSystem>
141  Cartesian2D & operator = (const CoordSystem & v) {
142  fX = v.x();
143  fY = v.y();
144  return *this;
145  }
146
147  /**
148  Exact equality
149  */
150  bool operator == (const Cartesian2D & rhs) const {
151  return fX == rhs.fX && fY == rhs.fY;
152  }
153  bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
154
155
156  // ============= Compatibility section ==================
157
158  // The following make this coordinate system look enough like a CLHEP
159  // vector that an assignment member template can work with either
160  Scalar x() const { return X();}
161  Scalar y() const { return Y();}
162
163  // ============= Overloads for improved speed ==================
164
165  template <class T2>
166  explicit Cartesian2D( const Polar2D<T2> & v )
167  {
168  Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
169  // is the speed improvement
170  fX = r * std::cos(v.Phi());
171  fY = r * std::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  Scalar r = v.R();
182  fX = r * std::cos(v.Phi());
183  fY = r * std::sin(v.Phi());
184  return *this;
185  }
186
187
188
189 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
190
191  // ====== Set member functions for coordinates in other systems =======
192
193  void SetR(Scalar r);
194
195  void SetPhi(Scalar phi);
196
197 #endif
198
199
200 private:
201
202  /**
203  (Contiguous) data containing the coordinates values x and y
204  */
207
208 };
209
210
211  } // end namespace Math
212
213 } // end namespace ROOT
214
215
216 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
217 // need to put here setter methods to resolve nasty cyclical dependencies
218 // I need to include other coordinate systems only when Cartesian is already defined
219 // since they depend on it
220
221 #ifndef ROOT_Math_GenVector_GenVector_exception
223 #endif
224 #ifndef ROOT_Math_GenVector_Polar2D
225 #include "Math/GenVector/Polar2D.h"
226 #endif
227
228 // ====== Set member functions for coordinates in other systems =======
229
230 namespace ROOT {
231
232  namespace Math {
233
234  template <class T>
236  GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
237  throw e;
238  Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
239  }
240
241
242  template <class T>
243  void Cartesian2D<T>::SetPhi(Scalar phi) {
244  GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
245  throw e;
246  Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
247  }
248
249
250
251  } // end namespace Math
252
253 } // end namespace ROOT
254
255 #endif
256
257
258
259
260 #endif /* ROOT_Math_GenVector_Cartesian2D */
Scalar X() const
Definition: Cartesian2D.h:93
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
Scalar R() const
Definition: Polar2D.h:102
return c
void SetY(Scalar a)
set the y coordinate value keeping x constant
Definition: Cartesian2D.h:107
double T(double x)
Definition: ChebyshevPol.h:34
Scalar Phi() const
Definition: Cartesian2D.h:97
void SetR(const T &r)
set the r coordinate value keeping phi constant
Definition: Polar2D.h:115
Scalar Y() const
Definition: Cartesian2D.h:94
void SetXY(Scalar xx, Scalar yy)
set all values using cartesian coordinates
Definition: Cartesian2D.h:112
void Scale(Scalar a)
scale the vector by a scalar quantity a
Definition: Cartesian2D.h:120
TArc * a
Definition: textangle.C:12
void SetPhi(const T &phi)
set the phi coordinate value keeping r constant
Definition: Polar2D.h:123
Scalar R() const
Definition: Cartesian2D.h:96
void Negate()
negate the vector
Definition: Cartesian2D.h:125
double cos(double)
Cartesian2D(const Cartesian2D &v)
copy constructor
Definition: Cartesian2D.h:71
double sqrt(double)
bool operator==(const Cartesian2D &rhs) const
Exact equality.
Definition: Cartesian2D.h:150
void Rotate(Scalar angle)
rotate by an angle
Definition: Cartesian2D.h:130
Scalar Phi() const
Definition: Polar2D.h:103
Class describing a polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition: Polar2D.h:47
double sin(double)
void SetX(Scalar a)
set the x coordinate value keeping y constant
Definition: Cartesian2D.h:102
bool operator!=(const Cartesian2D &rhs) const
Definition: Cartesian2D.h:153
TRandom2 r(17)
SVector< double, 2 > v
Definition: Dict.h:5
void SetCoordinates(Scalar xx, Scalar yy)
Set internal data based on 2 Scalar numbers.
Definition: Cartesian2D.h:86
Cartesian2D()
Default constructor with x=y=0.
Definition: Cartesian2D.h:50
Cartesian2D(const Polar2D< T2 > &v)
Definition: Cartesian2D.h:166
Cartesian2D & operator=(const Cartesian2D &v)
assignment operator
Definition: Cartesian2D.h:77
Cartesian2D(Scalar xx, Scalar yy)
Constructor from x,y coordinates.
Definition: Cartesian2D.h:55
double atan2(double, double)
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
Class describing a 2D cartesian coordinate system (x, y coordinates)
Definition: Cartesian2D.h:41
Namespace for new Math classes and functions.
T fX
(Contiguous) data containing the coordinates values x and y
Definition: Cartesian2D.h:205
Scalar Mag2() const
Definition: Cartesian2D.h:95
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition: Cartesian2D.h:91
Cartesian2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X() and Y()
Definition: Cartesian2D.h:62