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 @sa Overview of the @ref GenVector "physics vector library"
36 */
37
38template <class T = double>
40
41public :
42
43 typedef T Scalar;
44
45 /**
46 Default constructor with x=y=0
47 */
48 Cartesian2D() : fX(0.0), fY(0.0) { }
49
50 /**
51 Constructor from x,y coordinates
52 */
53 Cartesian2D(Scalar xx, Scalar yy) : fX(xx), fY(yy) { }
54
55 /**
56 Construct from any Vector or coordinate system implementing
57 X() and Y()
58 */
59 template <class CoordSystem>
60 explicit Cartesian2D(const CoordSystem & v)
61 : fX(v.X()), fY(v.Y()) { }
62
63
64 // for g++ 3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower
65 // re-implement them ( there is no no need to have them with g++4)
66 /**
67 copy constructor
68 */
70 fX(v.X()), fY(v.Y()) { }
71
72 /**
73 assignment operator
74 */
76 fX = v.X();
77 fY = v.Y();
78 return *this;
79 }
80
81 /**
82 Set internal data based on 2 Scalar numbers
83 */
84 void SetCoordinates(Scalar xx, Scalar yy) { fX=xx; fY=yy; }
85
86 /**
87 get internal data into 2 Scalar numbers
88 */
89 void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }
90
91 Scalar X() const { return fX;}
92 Scalar Y() const { return fY;}
93 Scalar Mag2() const { return fX*fX + fY*fY; }
94 Scalar R() const { using std::sqrt; return sqrt(Mag2()); }
95 Scalar Phi() const { using std::atan2; return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : atan2(fY, fX); }
96
97 /**
98 set the x coordinate value keeping y constant
99 */
100 void SetX(Scalar a) { fX = a; }
101
102 /**
103 set the y coordinate value keeping x constant
104 */
105 void SetY(Scalar a) { fY = a; }
106
107 /**
108 set all values using cartesian coordinates
109 */
110 void SetXY(Scalar xx, Scalar yy ) {
111 fX=xx;
112 fY=yy;
113 }
114
115 /**
116 scale the vector by a scalar quantity a
117 */
118 void Scale(Scalar a) { fX *= a; fY *= a; }
119
120 /**
121 negate the vector
122 */
123 void Negate() { fX = -fX; fY = -fY; }
124
125 /**
126 rotate by an angle
127 */
129 using std::sin;
130 const Scalar s = sin(angle);
131 using std::cos;
132 const Scalar c = 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 const Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
169 // is the speed improvement
170 using std::cos;
171 fX = r * cos(v.Phi());
172 using std::sin;
173 fY = r * sin(v.Phi());
174 }
175 // Technical note: This works even though only Polar2Dfwd.h is
176 // included (and in fact, including Polar2D.h would cause circularity
177 // problems). It works because any program **using** this ctor must itself
178 // be including Polar2D.h.
179
180 template <class T2>
182 {
183 const Scalar r = v.R();
184 using std::cos;
185 fX = r * cos(v.Phi());
186 using std::sin;
187 fY = r * sin(v.Phi());
188 return *this;
189 }
190
191
192
193#if defined(__MAKECINT__) || defined(G__DICTIONARY)
194
195 // ====== Set member functions for coordinates in other systems =======
196
197 void SetR(Scalar r);
198
199 void SetPhi(Scalar phi);
200
201#endif
202
203
204private:
205
206 /**
207 (Contiguous) data containing the coordinates values x and y
208 */
209 T fX;
210 T fY;
211
212};
213
214
215 } // end namespace Math
216
217} // end namespace ROOT
218
219
220#if defined(__MAKECINT__) || defined(G__DICTIONARY)
221// need to put here setter methods to resolve nasty cyclical dependencies
222// I need to include other coordinate systems only when Cartesian is already defined
223// since they depend on it
224
227
228// ====== Set member functions for coordinates in other systems =======
229
230namespace ROOT {
231
232 namespace Math {
233
234 template <class T>
235 void Cartesian2D<T>::SetR(Scalar r) {
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 */
#define c(i)
Definition RSha256.hxx:101
#define a(i)
Definition RSha256.hxx:99
#define e(i)
Definition RSha256.hxx:103
#define X(type, name)
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
Cartesian2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X() and Y()
Definition Cartesian2D.h:60
void SetX(Scalar a)
set the x coordinate value keeping y constant
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:69
Cartesian2D()
Default constructor with x=y=0.
Definition Cartesian2D.h:48
Cartesian2D(const Polar2D< T2 > &v)
Cartesian2D & operator=(const Cartesian2D &v)
assignment operator
Definition Cartesian2D.h:75
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition Cartesian2D.h:89
Scalar Mag2() const
Definition Cartesian2D.h:93
void SetCoordinates(Scalar xx, Scalar yy)
Set internal data based on 2 Scalar numbers.
Definition Cartesian2D.h:84
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:53
Class describing a polar 2D coordinate system based on r and phi Phi is restricted to be in the range...
Definition Polar2D.h:45
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
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.