ROOT   Reference Guide
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 static constexpr unsigned int Dimension = 2U;
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 constexpr 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 { using std::sqrt; return sqrt(Mag2()); }
97 Scalar Phi() const { using std::atan2; return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(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 */
131 using std::sin;
132 const Scalar s = sin(angle);
133 using std::cos;
134 const Scalar c = cos(angle);
135 SetCoordinates(c * fX - s * fY, s * fX + c * fY);
136 }
137
138 /**
139 Assignment from any class implementing x(),y()
140 (can assign from any coordinate system)
141 */
142 template <class CoordSystem>
143 Cartesian2D & operator = (const CoordSystem & v) {
144 fX = v.x();
145 fY = v.y();
146 return *this;
147 }
148
149 /**
150 Exact equality
151 */
152 bool operator == (const Cartesian2D & rhs) const {
153 return fX == rhs.fX && fY == rhs.fY;
154 }
155 bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
156
157
158 // ============= Compatibility section ==================
159
160 // The following make this coordinate system look enough like a CLHEP
161 // vector that an assignment member template can work with either
162 Scalar x() const { return X();}
163 Scalar y() const { return Y();}
164
165 // ============= Overloads for improved speed ==================
166
167 template <class T2>
168 explicit constexpr Cartesian2D( const Polar2D<T2> & v )
169 {
170 const Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
171 // is the speed improvement
172 using std::cos;
173 fX = r * cos(v.Phi());
174 using std::sin;
175 fY = r * sin(v.Phi());
176 }
177 // Technical note: This works even though only Polar2Dfwd.h is
178 // included (and in fact, including Polar2D.h would cause circularity
179 // problems). It works because any program **using** this ctor must itself
180 // be including Polar2D.h.
181
182 template <class T2>
184 {
185 const Scalar r = v.R();
186 using std::cos;
187 fX = r * cos(v.Phi());
188 using std::sin;
189 fY = r * sin(v.Phi());
190 return *this;
191 }
192
193
194
195#if defined(__MAKECINT__) || defined(G__DICTIONARY)
196
197 // ====== Set member functions for coordinates in other systems =======
198
199 void SetR(Scalar r);
200
201 void SetPhi(Scalar phi);
202
203#endif
204
205
206private:
207
208 /**
209 (Contiguous) data containing the coordinates values x and y
210 */
211 T fX;
212 T fY;
213
214};
215
216
217 } // end namespace Math
218
219} // end namespace ROOT
220
221
222#if defined(__MAKECINT__) || defined(G__DICTIONARY)
223// need to put here setter methods to resolve nasty cyclical dependencies
224// I need to include other coordinate systems only when Cartesian is already defined
225// since they depend on it
226
229
230// ====== Set member functions for coordinates in other systems =======
231
232namespace ROOT {
233
234 namespace Math {
235
236 template <class T>
237 void Cartesian2D<T>::SetR(Scalar r) {
238 GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
239 throw e;
240 Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
241 }
242
243
244 template <class T>
245 void Cartesian2D<T>::SetPhi(Scalar phi) {
246 GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
247 throw e;
248 Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
249 }
250
251
252
253 } // end namespace Math
254
255} // end namespace ROOT
256
257#endif
258
259
260
261
262#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
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:71
Cartesian2D()
Default constructor with x=y=0.
Definition Cartesian2D.h:50
static constexpr unsigned int Dimension
Definition Cartesian2D.h:45
Cartesian2D & operator=(const Cartesian2D &v)
assignment operator
Definition Cartesian2D.h:77
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition Cartesian2D.h:91
Scalar Mag2() const
Definition Cartesian2D.h:95
constexpr Cartesian2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X() and Y()
Definition Cartesian2D.h:62
constexpr Cartesian2D(const Polar2D< T2 > &v)
void SetCoordinates(Scalar xx, Scalar yy)
Set internal data based on 2 Scalar numbers.
Definition Cartesian2D.h:86
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:55
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
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...