File indexing completed on 2025-11-04 10:26:04
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    Cartesian2D() : fX(0.0), fY(0.0)  {  }
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    
0069 
0070 
0071    Cartesian2D(const Cartesian2D & v) :
0072       fX(v.X()), fY(v.Y())  {  }
0073 
0074    
0075 
0076 
0077    Cartesian2D & operator= (const Cartesian2D & v) {
0078       fX = v.X();
0079       fY = v.Y();
0080       return *this;
0081    }
0082 
0083    
0084 
0085 
0086    void SetCoordinates(Scalar  xx, Scalar  yy) { fX=xx; fY=yy;  }
0087 
0088    
0089 
0090 
0091    void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }
0092 
0093    Scalar X()     const { return fX;}
0094    Scalar Y()     const { return fY;}
0095    Scalar Mag2()  const { return fX*fX + fY*fY; }
0096    Scalar R() const { using std::sqrt; return sqrt(Mag2()); }
0097    Scalar Phi() const { using std::atan2; return (fX == Scalar(0) && fY == Scalar(0)) ? Scalar(0) : atan2(fY, fX); }
0098 
0099    
0100 
0101 
0102    void SetX(Scalar a) { fX = a; }
0103 
0104    
0105 
0106 
0107    void SetY(Scalar a) { fY = a; }
0108 
0109    
0110 
0111 
0112    void SetXY(Scalar xx, Scalar yy ) {
0113       fX=xx;
0114       fY=yy;
0115    }
0116 
0117    
0118 
0119 
0120    void Scale(Scalar a) { fX *= a; fY *= a;  }
0121 
0122    
0123 
0124 
0125    void Negate() { fX = -fX; fY = -fY;  }
0126 
0127    
0128 
0129 
0130    void Rotate(Scalar angle) {
0131       using std::sin;
0132       const Scalar s = sin(angle);
0133       using std::cos;
0134       const Scalar c = cos(angle);
0135       SetCoordinates(c * fX - s * fY, s * fX + c * fY);
0136    }
0137 
0138    
0139 
0140 
0141 
0142    template <class CoordSystem>
0143    Cartesian2D & operator = (const CoordSystem & v) {
0144       fX = v.x();
0145       fY = v.y();
0146       return *this;
0147    }
0148 
0149    
0150 
0151 
0152    bool operator == (const Cartesian2D & rhs) const {
0153       return fX == rhs.fX && fY == rhs.fY;
0154    }
0155    bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}
0156 
0157 
0158    
0159 
0160    
0161    
0162    Scalar x() const { return X();}
0163    Scalar y() const { return Y();}
0164 
0165    
0166 
0167    template <class T2>
0168    explicit constexpr Cartesian2D( const Polar2D<T2> & v )
0169    {
0170       const Scalar r = v.R(); 
0171       
0172       using std::cos;
0173       fX = r * cos(v.Phi());
0174       using std::sin;
0175       fY = r * sin(v.Phi());
0176    }
0177    
0178    
0179    
0180    
0181 
0182    template <class T2>
0183    Cartesian2D & operator = (const Polar2D<T2> & v)
0184    {
0185       const Scalar r = v.R();
0186       using std::cos;
0187       fX             = r * cos(v.Phi());
0188       using std::sin;
0189       fY             = r * sin(v.Phi());
0190       return *this;
0191    }
0192 
0193 
0194 
0195 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
0196 
0197    
0198 
0199    void SetR(Scalar r);
0200 
0201    void SetPhi(Scalar phi);
0202 
0203 #endif
0204 
0205 
0206 private:
0207 
0208    
0209 
0210 
0211    T  fX;
0212    T  fY;
0213 
0214 };
0215 
0216 
0217    } 
0218 
0219 } 
0220 
0221 
0222 #if defined(__MAKECINT__) || defined(G__DICTIONARY)
0223 
0224 
0225 
0226 
0227 #include "Math/GenVector/GenVector_exception.h"
0228 #include "Math/GenVector/Polar2D.h"
0229 
0230 
0231 
0232 namespace ROOT {
0233 
0234    namespace Math {
0235 
0236       template <class T>
0237       void Cartesian2D<T>::SetR(Scalar r) {
0238          GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
0239          throw e;
0240          Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
0241       }
0242 
0243 
0244       template <class T>
0245       void Cartesian2D<T>::SetPhi(Scalar phi) {
0246          GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
0247          throw e;
0248          Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
0249       }
0250 
0251 
0252 
0253    } 
0254 
0255 } 
0256 
0257 #endif
0258 
0259 
0260 
0261 
0262 #endif