Logo ROOT   6.13/01
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 
20 
21 #include "Math/Math.h"
22 
23 
24 namespace ROOT {
25 
26 namespace Math {
27 
28 //__________________________________________________________________________________________
29  /**
30  Class describing a 2D cartesian coordinate system
31  (x, y coordinates)
32 
33  @ingroup GenVector
34  */
35 
36 template <class T = double>
37 class Cartesian2D {
38 
39 public :
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 { return sqrt(Mag2()); }
93  Scalar Phi() const { 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  const Scalar s = sin(angle);
128  const Scalar c = cos(angle);
129  SetCoordinates(c * fX - s * fY, s * fX + c * fY);
130  }
131 
132  /**
133  Assignment from any class implementing x(),y()
134  (can assign from any coordinate system)
135  */
136  template <class CoordSystem>
137  Cartesian2D & operator = (const CoordSystem & v) {
138  fX = v.x();
139  fY = v.y();
140  return *this;
141  }
142 
143  /**
144  Exact equality
145  */
146  bool operator == (const Cartesian2D & rhs) const {
147  return fX == rhs.fX && fY == rhs.fY;
148  }
149  bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
150 
151 
152  // ============= Compatibility section ==================
153 
154  // The following make this coordinate system look enough like a CLHEP
155  // vector that an assignment member template can work with either
156  Scalar x() const { return X();}
157  Scalar y() const { return Y();}
158 
159  // ============= Overloads for improved speed ==================
160 
161  template <class T2>
162  explicit Cartesian2D( const Polar2D<T2> & v )
163  {
164  const Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
165  // is the speed improvement
166  fX = r * std::cos(v.Phi());
167  fY = r * std::sin(v.Phi());
168  }
169  // Technical note: This works even though only Polar2Dfwd.h is
170  // included (and in fact, including Polar2D.h would cause circularity
171  // problems). It works because any program **using** this ctor must itself
172  // be including Polar2D.h.
173 
174  template <class T2>
176  {
177  const Scalar r = v.R();
178  fX = r * cos(v.Phi());
179  fY = r * sin(v.Phi());
180  return *this;
181  }
182 
183 
184 
185 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
186 
187  // ====== Set member functions for coordinates in other systems =======
188 
189  void SetR(Scalar r);
190 
191  void SetPhi(Scalar phi);
192 
193 #endif
194 
195 
196 private:
197 
198  /**
199  (Contiguous) data containing the coordinates values x and y
200  */
201  T fX;
202  T fY;
203 
204 };
205 
206 
207  } // end namespace Math
208 
209 } // end namespace ROOT
210 
211 
212 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
213 // need to put here setter methods to resolve nasty cyclical dependencies
214 // I need to include other coordinate systems only when Cartesian is already defined
215 // since they depend on it
216 
218 #include "Math/GenVector/Polar2D.h"
219 
220 // ====== Set member functions for coordinates in other systems =======
221 
222 namespace ROOT {
223 
224  namespace Math {
225 
226  template <class T>
227  void Cartesian2D<T>::SetR(Scalar r) {
228  GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
229  throw e;
230  Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
231  }
232 
233 
234  template <class T>
235  void Cartesian2D<T>::SetPhi(Scalar phi) {
236  GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
237  throw e;
238  Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
239  }
240 
241 
242 
243  } // end namespace Math
244 
245 } // end namespace ROOT
246 
247 #endif
248 
249 
250 
251 
252 #endif /* ROOT_Math_GenVector_Cartesian2D */
Scalar X() const
Definition: Cartesian2D.h:89
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
Scalar R() const
Definition: Polar2D.h:98
void SetY(Scalar a)
set the y coordinate value keeping x constant
Definition: Cartesian2D.h:103
double T(double x)
Definition: ChebyshevPol.h:34
Scalar Phi() const
Definition: Cartesian2D.h:93
void SetR(const T &r)
set the r coordinate value keeping phi constant
Definition: Polar2D.h:111
Scalar Y() const
Definition: Cartesian2D.h:90
void SetXY(Scalar xx, Scalar yy)
set all values using cartesian coordinates
Definition: Cartesian2D.h:108
void Scale(Scalar a)
scale the vector by a scalar quantity a
Definition: Cartesian2D.h:116
void SetPhi(const T &phi)
set the phi coordinate value keeping r constant
Definition: Polar2D.h:119
Scalar R() const
Definition: Cartesian2D.h:92
void Negate()
negate the vector
Definition: Cartesian2D.h:121
double cos(double)
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
Cartesian2D(const Cartesian2D &v)
copy constructor
Definition: Cartesian2D.h:67
bool operator==(const Cartesian2D &rhs) const
Exact equality.
Definition: Cartesian2D.h:146
void Rotate(Scalar angle)
rotate by an angle
Definition: Cartesian2D.h:126
Scalar Phi() const
Definition: Polar2D.h:99
Class describing a polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition: Polar2D.h:43
double sin(double)
void SetX(Scalar a)
set the x coordinate value keeping y constant
Definition: Cartesian2D.h:98
bool operator!=(const Cartesian2D &rhs) const
Definition: Cartesian2D.h:149
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:82
Cartesian2D()
Default constructor with x=y=0.
Definition: Cartesian2D.h:46
Cartesian2D(const Polar2D< T2 > &v)
Definition: Cartesian2D.h:162
Cartesian2D & operator=(const Cartesian2D &v)
assignment operator
Definition: Cartesian2D.h:73
Cartesian2D(Scalar xx, Scalar yy)
Constructor from x,y coordinates.
Definition: Cartesian2D.h:51
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:37
Namespace for new Math classes and functions.
T fX
(Contiguous) data containing the coordinates values x and y
Definition: Cartesian2D.h:201
Scalar Mag2() const
Definition: Cartesian2D.h:91
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition: Cartesian2D.h:87
Cartesian2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X() and Y()
Definition: Cartesian2D.h:58