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_MathX_GenVectorX_Cartesian2D
17#define ROOT_MathX_GenVectorX_Cartesian2D 1
18
20
21#include "Math/Math.h"
22
24
26
27using namespace ROOT::ROOT_MATH_ARCH;
28
29namespace ROOT {
30
31namespace ROOT_MATH_ARCH {
32
33//__________________________________________________________________________________________
34/**
35 Class describing a 2D cartesian coordinate system
36 (x, y coordinates)
37
38 @ingroup GenVectorX
39
40 @see GenVectorX
41*/
42
43template <class T = double>
45
46public:
47 typedef T Scalar;
48
49 /**
50 Default constructor with x=y=0
51 */
53
54 /**
55 Constructor from x,y coordinates
56 */
58
59 /**
60 Construct from any Vector or coordinate system implementing
61 X() and Y()
62 */
63 template <class CoordSystem>
64 explicit constexpr Cartesian2D(const CoordSystem &v) : fX(v.X()), fY(v.Y())
65 {
66 }
67
68 /**
69 Set internal data based on 2 Scalar numbers
70 */
72 {
73 fX = xx;
74 fY = yy;
75 }
76
77 /**
78 get internal data into 2 Scalar numbers
79 */
81 {
82 xx = fX;
83 yy = fY;
84 }
85
86 Scalar X() const { return fX; }
87 Scalar Y() const { return fY; }
88 Scalar Mag2() const { return fX * fX + fY * fY; }
89 Scalar R() const { return math_sqrt(Mag2()); }
90 Scalar Phi() const { return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : math_atan2(fY, fX); }
91
92 /**
93 set the x coordinate value keeping y constant
94 */
95 void SetX(Scalar a) { fX = a; }
96
97 /**
98 set the y coordinate value keeping x constant
99 */
100 void SetY(Scalar a) { fY = a; }
101
102 /**
103 set all values using cartesian coordinates
104 */
106 {
107 fX = xx;
108 fY = yy;
109 }
110
111 /**
112 scale the vector by a scalar quantity a
113 */
115 {
116 fX *= a;
117 fY *= a;
118 }
119
120 /**
121 negate the vector
122 */
123 void Negate()
124 {
125 fX = -fX;
126 fY = -fY;
127 }
128
129 /**
130 rotate by an angle
131 */
133 {
134 const Scalar s = math_sin(angle);
135 const Scalar c = math_cos(angle);
136 SetCoordinates(c * fX - s * fY, s * fX + c * fY);
137 }
138
139 /**
140 Assignment from any class implementing x(),y()
141 (can assign from any coordinate system)
142 */
143 template <class CoordSystem>
144 Cartesian2D &operator=(const CoordSystem &v)
145 {
146 fX = v.x();
147 fY = v.y();
148 return *this;
149 }
150
151 /**
152 Exact equality
153 */
154 bool operator==(const Cartesian2D &rhs) const { return fX == rhs.fX && fY == rhs.fY; }
155 bool operator!=(const Cartesian2D &rhs) const { return !(operator==(rhs)); }
156
157 // ============= Compatibility section ==================
158
159 // The following make this coordinate system look enough like a CLHEP
160 // vector that an assignment member template can work with either
161 Scalar x() const { return X(); }
162 Scalar y() const { return Y(); }
163
164 // ============= Overloads for improved speed ==================
165
166 template <class T2>
167 explicit Cartesian2D(const Polar2D<T2> &v)
168 {
169 const Scalar r = v.R(); // re-using this instead of calling v.X() and v.Y()
170 // is the speed improvement
171 fX = r * math_cos(v.Phi());
172 fY = r * math_sin(v.Phi());
173 }
174 // Technical note: This works even though only Polar2Dfwd.h is
175 // included (and in fact, including Polar2D.h would cause circularity
176 // problems). It works because any program **using** this ctor must itself
177 // be including Polar2D.h.
178
179 template <class T2>
181 {
182 const Scalar r = v.R();
183 fX = r * math_cos(v.Phi());
184 fY = r * math_sin(v.Phi());
185 return *this;
186 }
187
188#if defined(__MAKECINT__) || defined(G__DICTIONARY)
189
190 // ====== Set member functions for coordinates in other systems =======
191
192 void SetR(Scalar r);
193
194 void SetPhi(Scalar phi);
195
196#endif
197
198private:
199 /**
200 (Contiguous) data containing the coordinates values x and y
201 */
202 T fX = 0;
203 T fY = 0;
204};
205
206} // end namespace ROOT_MATH_ARCH
207
208} // end namespace ROOT
209
210#if defined(__MAKECINT__) || defined(G__DICTIONARY)
211#if !defined(ROOT_MATH_SYCL) && !defined(ROOT_MATH_CUDA)
212// need to put here setter methods to resolve nasty cyclical dependencies
213// I need to include other coordinate systems only when Cartesian is already defined
214// since they depend on it
215
218
219// ====== Set member functions for coordinates in other systems =======
220
221namespace ROOT {
222
223namespace ROOT_MATH_ARCH {
224
225template <class T>
227{
228 GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
229 throw e;
230 Polar2D<Scalar> v(*this);
231 v.SetR(r);
232 *this = Cartesian2D<Scalar>(v);
233}
234
235template <class T>
237{
238 GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
239 throw e;
240 Polar2D<Scalar> v(*this);
241 v.SetPhi(phi);
242 *this = Cartesian2D<Scalar>(v);
243}
244
245} // namespace ROOT_MATH_ARCH
246
247} // end namespace ROOT
248
249#endif
250#endif
251
252#endif /* ROOT_MathX_GenVectorX_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:44
Cartesian2D & operator=(const Polar2D< T2 > &v)
void SetX(Scalar a)
set the x coordinate value keeping y constant
Definition Cartesian2D.h:95
void SetY(Scalar a)
set the y coordinate value keeping x constant
T fX
(Contiguous) data containing the coordinates values x and y
Cartesian2D(const Polar2D< T2 > &v)
bool operator!=(const Cartesian2D &rhs) const
Cartesian2D & operator=(const CoordSystem &v)
Assignment from any class implementing x(),y() (can assign from any coordinate system)
void Rotate(Scalar angle)
rotate by an angle
void SetXY(Scalar xx, Scalar yy)
set all values using cartesian coordinates
bool operator==(const Cartesian2D &rhs) const
Exact equality.
void GetCoordinates(Scalar &xx, Scalar &yy) const
get internal data into 2 Scalar numbers
Definition Cartesian2D.h:80
void Negate()
negate the vector
void Scale(Scalar a)
scale the vector by a scalar quantity a
constexpr Cartesian2D(const CoordSystem &v)
Construct from any Vector or coordinate system implementing X() and Y()
Definition Cartesian2D.h:64
constexpr Cartesian2D() noexcept=default
Default constructor with x=y=0.
void SetCoordinates(Scalar xx, Scalar yy)
Set internal data based on 2 Scalar numbers.
Definition Cartesian2D.h:71
Scalar math_cos(Scalar x)
Scalar math_sqrt(Scalar x)
Scalar math_atan2(Scalar x, Scalar y)
Scalar math_sin(Scalar x)