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 @see GenVector
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 */
51
52 /**
53 Constructor from x,y coordinates
54 */
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 Set internal data based on 2 Scalar numbers
67 */
69
70 /**
71 get internal data into 2 Scalar numbers
72 */
73 void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }
74
75 Scalar X() const { return fX;}
76 Scalar Y() const { return fY;}
77 Scalar Mag2() const { return fX*fX + fY*fY; }
78 Scalar R() const { using std::sqrt; return sqrt(Mag2()); }
79 Scalar Phi() const { using std::atan2; return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : atan2(fY, fX); }
80
81 /**
82 set the x coordinate value keeping y constant
83 */
84 void SetX(Scalar a) { fX = a; }
85
86 /**
87 set the y coordinate value keeping x constant
88 */
89 void SetY(Scalar a) { fY = a; }
90
91 /**
92 set all values using cartesian coordinates
93 */
95 fX=xx;
96 fY=yy;
97 }
98
99 /**
100 scale the vector by a scalar quantity a
101 */
102 void Scale(Scalar a) { fX *= a; fY *= a; }
103
104 /**
105 negate the vector
106 */
107 void Negate() { fX = -fX; fY = -fY; }
108
109 /**
110 rotate by an angle
111 */
113 using std::sin;
114 const Scalar s = sin(angle);
115 using std::cos;
116 const Scalar c = cos(angle);
117 SetCoordinates(c * fX - s * fY, s * fX + c * fY);
118 }
119
120 /**
121 Assignment from any class implementing x(),y()
122 (can assign from any coordinate system)
123 */
124 template <class CoordSystem>
125 Cartesian2D & operator = (const CoordSystem & v) {
126 fX = v.x();
127 fY = v.y();
128 return *this;
129 }
130
131 /**
132 Exact equality
133 */
134 bool operator == (const Cartesian2D & rhs) const {
135 return fX == rhs.fX && fY == rhs.fY;
136 }
137 bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
138
139
140 // ============= Compatibility section ==================
141
142 // The following make this coordinate system look enough like a CLHEP
143 // vector that an assignment member template can work with either
144 Scalar x() const { return X();}
145 Scalar y() const { return Y();}
146
147 // ============= Overloads for improved speed ==================
148
149 template <class T2>
150 explicit constexpr Cartesian2D( const Polar2D<T2> & v )
151 {
152 const Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
153 // is the speed improvement
154 using std::cos;
155 fX = r * cos(v.Phi());
156 using std::sin;
157 fY = r * sin(v.Phi());
158 }
159 // Technical note: This works even though only Polar2Dfwd.h is
160 // included (and in fact, including Polar2D.h would cause circularity
161 // problems). It works because any program **using** this ctor must itself
162 // be including Polar2D.h.
163
164 template <class T2>
166 {
167 const Scalar r = v.R();
168 using std::cos;
169 fX = r * cos(v.Phi());
170 using std::sin;
171 fY = r * sin(v.Phi());
172 return *this;
173 }
174
175
176
177#if defined(__MAKECINT__) || defined(G__DICTIONARY)
178
179 // ====== Set member functions for coordinates in other systems =======
180
181 void SetR(Scalar r);
182
183 void SetPhi(Scalar phi);
184
185#endif
186
187
188private:
189
190 /**
191 (Contiguous) data containing the coordinates values x and y
192 */
193 T fX = 0;
194 T fY = 0;
195};
196
197
198 } // end namespace Math
199
200} // end namespace ROOT
201
202
203#if defined(__MAKECINT__) || defined(G__DICTIONARY)
204// need to put here setter methods to resolve nasty cyclical dependencies
205// I need to include other coordinate systems only when Cartesian is already defined
206// since they depend on it
207
210
211// ====== Set member functions for coordinates in other systems =======
212
213namespace ROOT {
214
215 namespace Math {
216
217 template <class T>
218 void Cartesian2D<T>::SetR(Scalar r) {
219 GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
220 throw e;
221 Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
222 }
223
224
225 template <class T>
226 void Cartesian2D<T>::SetPhi(Scalar phi) {
227 GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
228 throw e;
229 Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
230 }
231
232
233
234 } // end namespace Math
235
236} // end namespace ROOT
237
238#endif
239
240
241
242
243#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)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
Definition Cartesian2D.h:84
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
Definition Cartesian2D.h:89
void SetXY(Scalar xx, Scalar yy)
set all values using cartesian coordinates
Definition Cartesian2D.h:94
static constexpr unsigned int Dimension
Definition Cartesian2D.h:45
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition Cartesian2D.h:73
Scalar Mag2() const
Definition Cartesian2D.h:77
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)
Cartesian2D & operator=(const CoordSystem &v)
Assignment from any class implementing x(),y() (can assign from any coordinate system)
void SetCoordinates(Scalar xx, Scalar yy)
Set internal data based on 2 Scalar numbers.
Definition Cartesian2D.h:68
T fX
(Contiguous) data containing the coordinates values x and y
constexpr Cartesian2D() noexcept=default
Default constructor with x=y=0.
void Rotate(Scalar angle)
rotate by an angle
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
Namespace for new ROOT classes and functions.