File indexing completed on 2025-12-15 10:28:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef ROOT_Math_GenVector_Cartesian2D
0017 #define ROOT_Math_GenVector_Cartesian2D 1
0018
0019 #include "Math/GenVector/Polar2Dfwd.h"
0020
0021 #include "Math/Math.h"
0022
0023
0024 namespace ROOT {
0025
0026 namespace Math {
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 template <class T = double>
0039 class Cartesian2D {
0040
0041 public :
0042
0043 typedef T Scalar;
0044
0045 static constexpr unsigned int Dimension = 2U;
0046
0047
0048
0049
0050 constexpr Cartesian2D() noexcept = default;
0051
0052
0053
0054
0055 Cartesian2D(Scalar xx, Scalar yy) : fX(xx), fY(yy) { }
0056
0057
0058
0059
0060
0061 template <class CoordSystem>
0062 explicit constexpr Cartesian2D(const CoordSystem & v)
0063 : fX(v.X()), fY(v.Y()) { }
0064
0065
0066
0067
0068 void SetCoordinates(Scalar xx, Scalar yy) { fX=xx; fY=yy; }
0069
0070
0071
0072
0073 void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }
0074
0075 Scalar X() const { return fX;}
0076 Scalar Y() const { return fY;}
0077 Scalar Mag2() const { return fX*fX + fY*fY; }
0078 Scalar R() const { using std::sqrt; return sqrt(Mag2()); }
0079 Scalar Phi() const { using std::atan2; return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : atan2(fY, fX); }
0080
0081
0082
0083
0084 void SetX(Scalar a) { fX = a; }
0085
0086
0087
0088
0089 void SetY(Scalar a) { fY = a; }
0090
0091
0092
0093
0094 void SetXY(Scalar xx, Scalar yy ) {
0095 fX=xx;
0096 fY=yy;
0097 }
0098
0099
0100
0101
0102 void Scale(Scalar a) { fX *= a; fY *= a; }
0103
0104
0105
0106
0107 void Negate() { fX = -fX; fY = -fY; }
0108
0109
0110
0111
0112 void Rotate(Scalar angle) {
0113 using std::sin;
0114 const Scalar s = sin(angle);
0115 using std::cos;
0116 const Scalar c = cos(angle);
0117 SetCoordinates(c * fX - s * fY, s * fX + c * fY);
0118 }
0119
0120
0121
0122
0123
0124 template <class CoordSystem>
0125 Cartesian2D & operator = (const CoordSystem & v) {
0126 fX = v.x();
0127 fY = v.y();
0128 return *this;
0129 }
0130
0131
0132
0133
0134 bool operator == (const Cartesian2D & rhs) const {
0135 return fX == rhs.fX && fY == rhs.fY;
0136 }
0137 bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
0138
0139
0140
0141
0142
0143
0144 Scalar x() const { return X();}
0145 Scalar y() const { return Y();}
0146
0147
0148
0149 template <class T2>
0150 explicit constexpr Cartesian2D( const Polar2D<T2> & v )
0151 {
0152 const Scalar r = v.R();
0153
0154 using std::cos;
0155 fX = r * cos(v.Phi());
0156 using std::sin;
0157 fY = r * sin(v.Phi());
0158 }
0159
0160
0161
0162
0163
0164 template <class T2>
0165 Cartesian2D & operator = (const Polar2D<T2> & v)
0166 {
0167 const Scalar r = v.R();
0168 using std::cos;
0169 fX = r * cos(v.Phi());
0170 using std::sin;
0171 fY = r * sin(v.Phi());
0172 return *this;
0173 }
0174
0175
0176
0177 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
0178
0179
0180
0181 void SetR(Scalar r);
0182
0183 void SetPhi(Scalar phi);
0184
0185 #endif
0186
0187
0188 private:
0189
0190
0191
0192
0193 T fX = 0;
0194 T fY = 0;
0195 };
0196
0197
0198 }
0199
0200 }
0201
0202
0203 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
0204
0205
0206
0207
0208 #include "Math/GenVector/GenVector_exception.h"
0209 #include "Math/GenVector/Polar2D.h"
0210
0211
0212
0213 namespace ROOT {
0214
0215 namespace Math {
0216
0217 template <class T>
0218 void Cartesian2D<T>::SetR(Scalar r) {
0219 GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
0220 throw e;
0221 Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
0222 }
0223
0224
0225 template <class T>
0226 void Cartesian2D<T>::SetPhi(Scalar phi) {
0227 GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
0228 throw e;
0229 Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
0230 }
0231
0232
0233
0234 }
0235
0236 }
0237
0238 #endif
0239
0240
0241
0242
0243 #endif