ROOT logo
// @(#)root/mathcore:$Id: Cartesian2D.h 24923 2008-07-23 15:43:05Z moneta $
// Authors: W. Brown, M. Fischler, L. Moneta    2005  

 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2005 , LCG ROOT MathLib Team 			       *
  *                    & FNAL LCG ROOT Mathlib Team  		       *
  *                                                                    *
  *                                                                    *
  **********************************************************************/

// Header file for class Cartesian2D
// 
// Created by: Lorenzo Moneta  at Mon 16 Apr 2007
// 
#ifndef ROOT_Math_GenVector_Cartesian2D 
#define ROOT_Math_GenVector_Cartesian2D  1

#ifndef ROOT_Math_GenVector_Polar2Dfwd 
#include "Math/GenVector/Polar2Dfwd.h"
#endif

#ifndef ROOT_Math_Math
#include "Math/Math.h"
#endif


namespace ROOT { 

namespace Math { 

//__________________________________________________________________________________________
   /** 
       Class describing a 2D cartesian coordinate system
       (x, y coordinates) 
       
       @ingroup GenVector
   */ 

template <class T = double> 
class Cartesian2D { 

public : 

   typedef T Scalar;

   /**
      Default constructor  with x=y=0 
   */
   Cartesian2D() : fX(0), fY(0)  {  }

   /**
      Constructor from x,y  coordinates
   */
   Cartesian2D(Scalar xx, Scalar yy) : fX(xx), fY(yy) {  } 

   /**
      Construct from any Vector or coordinate system implementing 
      X() and Y() 
   */
   template <class CoordSystem> 
   explicit Cartesian2D(const CoordSystem & v) 
      : fX(v.X()), fY(v.Y()) {  }


   // for g++  3.2 and 3.4 on 32 bits found that the compiler generated copy ctor and assignment are much slower 
   // re-implement them ( there is no no need to have them with g++4)
   /**
      copy constructor
    */
   Cartesian2D(const Cartesian2D & v) :
      fX(v.X()), fY(v.Y())  {  }

   /**
      assignment operator 
    */
   Cartesian2D & operator= (const Cartesian2D & v) { 
      fX = v.X();  
      fY = v.Y();  
      return *this;
   }

   /**
      Set internal data based on 2 Scalar numbers
   */ 
   void SetCoordinates(Scalar  xx, Scalar  yy) { fX=xx; fY=yy;  }

   /**
      get internal data into 2 Scalar numbers
   */ 
   void GetCoordinates(Scalar& xx, Scalar& yy ) const {xx=fX; yy=fY; }  

   Scalar X()     const { return fX;}
   Scalar Y()     const { return fY;}
   Scalar Mag2()  const { return fX*fX + fY*fY; }
   Scalar R()     const { return std::sqrt( Mag2());}
   Scalar Phi()   const { return (fX==0 && fY==0) ? 0.0 : atan2(fY,fX);}
 
   /** 
       set the x coordinate value keeping y constant
   */ 
   void SetX(Scalar a) { fX = a; }

   /** 
       set the y coordinate value keeping x constant
   */ 
   void SetY(Scalar a) { fY = a; }

   /** 
       set all values using cartesian coordinates  
   */
   void SetXY(Scalar xx, Scalar yy ) { 
      fX=xx; 
      fY=yy; 
   }
 
   /**
      scale the vector by a scalar quantity a
   */
   void Scale(Scalar a) { fX *= a; fY *= a;  }

   /**
      negate the vector
   */
   void Negate() { fX = -fX; fY = -fY;  }

   /** 
       rotate by an angle
    */ 
   void Rotate(Scalar angle) { 
      Scalar s = std::sin(angle);
      Scalar c = std::cos(angle);
      SetCoordinates( c*fX - s*fY, s*fX + c * fY );
   }

   /**
      Assignment from any class implementing x(),y() 
      (can assign from any coordinate system) 
   */
   template <class CoordSystem> 
   Cartesian2D & operator = (const CoordSystem & v) { 
      fX = v.x();  
      fY = v.y();  
      return *this;
   }

   /**
      Exact equality
   */  
   bool operator == (const Cartesian2D & rhs) const {
      return fX == rhs.fX && fY == rhs.fY;
   }
   bool operator != (const Cartesian2D & rhs) const {return !(operator==(rhs));}

  
   // ============= Compatibility section ==================
  
   // The following make this coordinate system look enough like a CLHEP
   // vector that an assignment member template can work with either
   Scalar x() const { return X();}
   Scalar y() const { return Y();}
  
   // ============= Overloads for improved speed ==================

   template <class T2>
   explicit Cartesian2D( const Polar2D<T2> & v ) 
   {
      Scalar r = v.R();    // re-using this instead of calling v.X() and v.Y()
      // is the speed improvement
      fX = r * std::cos(v.Phi());
      fY = r * std::sin(v.Phi());    
   } 
   // Technical note:  This works even though only Polar2Dfwd.h is 
   // included (and in fact, including Polar2D.h would cause circularity
   // problems). It works because any program **using** this ctor must itself
   // be including Polar2D.h.
    
   template <class T2>
   Cartesian2D & operator = (const Polar2D<T2> & v) 
   { 
      Scalar r = v.R(); 
      fX = r * std::cos(v.Phi());
      fY = r * std::sin(v.Phi());
      return *this;
   }



#if defined(__MAKECINT__) || defined(G__DICTIONARY) 

   // ====== Set member functions for coordinates in other systems =======

   void SetR(Scalar r);
  
   void SetPhi(Scalar phi); 
  
#endif


private:

   /**
      (Contiguous) data containing the coordinates values x and y
   */
   T  fX;
   T  fY;

};


   } // end namespace Math

} // end namespace ROOT


#if defined(__MAKECINT__) || defined(G__DICTIONARY) 
// need to put here setter methods to resolve nasty cyclical dependencies 
// I need to include other coordinate systems only when Cartesian is already defined 
// since they depend on it

#include "Math/GenVector/GenVector_exception.h"
#include "Math/GenVector/Polar2D.h"

// ====== Set member functions for coordinates in other systems =======

namespace ROOT { 

   namespace Math { 

      template <class T>  
      void Cartesian2D<T>::SetR(Scalar r) {  
         GenVector_exception e("Cartesian2D::SetR() is not supposed to be called");
         throw e;
         Polar2D<Scalar> v(*this); v.SetR(r); *this = Cartesian2D<Scalar>(v);
      }


      template <class T>  
      void Cartesian2D<T>::SetPhi(Scalar phi) {  
         GenVector_exception e("Cartesian2D::SetPhi() is not supposed to be called");
         throw e;
         Polar2D<Scalar> v(*this); v.SetPhi(phi); *this = Cartesian2D<Scalar>(v);
      }



   } // end namespace Math

} // end namespace ROOT

#endif




#endif /* ROOT_Math_GenVector_Cartesian2D  */
 Cartesian2D.h:1
 Cartesian2D.h:2
 Cartesian2D.h:3
 Cartesian2D.h:4
 Cartesian2D.h:5
 Cartesian2D.h:6
 Cartesian2D.h:7
 Cartesian2D.h:8
 Cartesian2D.h:9
 Cartesian2D.h:10
 Cartesian2D.h:11
 Cartesian2D.h:12
 Cartesian2D.h:13
 Cartesian2D.h:14
 Cartesian2D.h:15
 Cartesian2D.h:16
 Cartesian2D.h:17
 Cartesian2D.h:18
 Cartesian2D.h:19
 Cartesian2D.h:20
 Cartesian2D.h:21
 Cartesian2D.h:22
 Cartesian2D.h:23
 Cartesian2D.h:24
 Cartesian2D.h:25
 Cartesian2D.h:26
 Cartesian2D.h:27
 Cartesian2D.h:28
 Cartesian2D.h:29
 Cartesian2D.h:30
 Cartesian2D.h:31
 Cartesian2D.h:32
 Cartesian2D.h:33
 Cartesian2D.h:34
 Cartesian2D.h:35
 Cartesian2D.h:36
 Cartesian2D.h:37
 Cartesian2D.h:38
 Cartesian2D.h:39
 Cartesian2D.h:40
 Cartesian2D.h:41
 Cartesian2D.h:42
 Cartesian2D.h:43
 Cartesian2D.h:44
 Cartesian2D.h:45
 Cartesian2D.h:46
 Cartesian2D.h:47
 Cartesian2D.h:48
 Cartesian2D.h:49
 Cartesian2D.h:50
 Cartesian2D.h:51
 Cartesian2D.h:52
 Cartesian2D.h:53
 Cartesian2D.h:54
 Cartesian2D.h:55
 Cartesian2D.h:56
 Cartesian2D.h:57
 Cartesian2D.h:58
 Cartesian2D.h:59
 Cartesian2D.h:60
 Cartesian2D.h:61
 Cartesian2D.h:62
 Cartesian2D.h:63
 Cartesian2D.h:64
 Cartesian2D.h:65
 Cartesian2D.h:66
 Cartesian2D.h:67
 Cartesian2D.h:68
 Cartesian2D.h:69
 Cartesian2D.h:70
 Cartesian2D.h:71
 Cartesian2D.h:72
 Cartesian2D.h:73
 Cartesian2D.h:74
 Cartesian2D.h:75
 Cartesian2D.h:76
 Cartesian2D.h:77
 Cartesian2D.h:78
 Cartesian2D.h:79
 Cartesian2D.h:80
 Cartesian2D.h:81
 Cartesian2D.h:82
 Cartesian2D.h:83
 Cartesian2D.h:84
 Cartesian2D.h:85
 Cartesian2D.h:86
 Cartesian2D.h:87
 Cartesian2D.h:88
 Cartesian2D.h:89
 Cartesian2D.h:90
 Cartesian2D.h:91
 Cartesian2D.h:92
 Cartesian2D.h:93
 Cartesian2D.h:94
 Cartesian2D.h:95
 Cartesian2D.h:96
 Cartesian2D.h:97
 Cartesian2D.h:98
 Cartesian2D.h:99
 Cartesian2D.h:100
 Cartesian2D.h:101
 Cartesian2D.h:102
 Cartesian2D.h:103
 Cartesian2D.h:104
 Cartesian2D.h:105
 Cartesian2D.h:106
 Cartesian2D.h:107
 Cartesian2D.h:108
 Cartesian2D.h:109
 Cartesian2D.h:110
 Cartesian2D.h:111
 Cartesian2D.h:112
 Cartesian2D.h:113
 Cartesian2D.h:114
 Cartesian2D.h:115
 Cartesian2D.h:116
 Cartesian2D.h:117
 Cartesian2D.h:118
 Cartesian2D.h:119
 Cartesian2D.h:120
 Cartesian2D.h:121
 Cartesian2D.h:122
 Cartesian2D.h:123
 Cartesian2D.h:124
 Cartesian2D.h:125
 Cartesian2D.h:126
 Cartesian2D.h:127
 Cartesian2D.h:128
 Cartesian2D.h:129
 Cartesian2D.h:130
 Cartesian2D.h:131
 Cartesian2D.h:132
 Cartesian2D.h:133
 Cartesian2D.h:134
 Cartesian2D.h:135
 Cartesian2D.h:136
 Cartesian2D.h:137
 Cartesian2D.h:138
 Cartesian2D.h:139
 Cartesian2D.h:140
 Cartesian2D.h:141
 Cartesian2D.h:142
 Cartesian2D.h:143
 Cartesian2D.h:144
 Cartesian2D.h:145
 Cartesian2D.h:146
 Cartesian2D.h:147
 Cartesian2D.h:148
 Cartesian2D.h:149
 Cartesian2D.h:150
 Cartesian2D.h:151
 Cartesian2D.h:152
 Cartesian2D.h:153
 Cartesian2D.h:154
 Cartesian2D.h:155
 Cartesian2D.h:156
 Cartesian2D.h:157
 Cartesian2D.h:158
 Cartesian2D.h:159
 Cartesian2D.h:160
 Cartesian2D.h:161
 Cartesian2D.h:162
 Cartesian2D.h:163
 Cartesian2D.h:164
 Cartesian2D.h:165
 Cartesian2D.h:166
 Cartesian2D.h:167
 Cartesian2D.h:168
 Cartesian2D.h:169
 Cartesian2D.h:170
 Cartesian2D.h:171
 Cartesian2D.h:172
 Cartesian2D.h:173
 Cartesian2D.h:174
 Cartesian2D.h:175
 Cartesian2D.h:176
 Cartesian2D.h:177
 Cartesian2D.h:178
 Cartesian2D.h:179
 Cartesian2D.h:180
 Cartesian2D.h:181
 Cartesian2D.h:182
 Cartesian2D.h:183
 Cartesian2D.h:184
 Cartesian2D.h:185
 Cartesian2D.h:186
 Cartesian2D.h:187
 Cartesian2D.h:188
 Cartesian2D.h:189
 Cartesian2D.h:190
 Cartesian2D.h:191
 Cartesian2D.h:192
 Cartesian2D.h:193
 Cartesian2D.h:194
 Cartesian2D.h:195
 Cartesian2D.h:196
 Cartesian2D.h:197
 Cartesian2D.h:198
 Cartesian2D.h:199
 Cartesian2D.h:200
 Cartesian2D.h:201
 Cartesian2D.h:202
 Cartesian2D.h:203
 Cartesian2D.h:204
 Cartesian2D.h:205
 Cartesian2D.h:206
 Cartesian2D.h:207
 Cartesian2D.h:208
 Cartesian2D.h:209
 Cartesian2D.h:210
 Cartesian2D.h:211
 Cartesian2D.h:212
 Cartesian2D.h:213
 Cartesian2D.h:214
 Cartesian2D.h:215
 Cartesian2D.h:216
 Cartesian2D.h:217
 Cartesian2D.h:218
 Cartesian2D.h:219
 Cartesian2D.h:220
 Cartesian2D.h:221
 Cartesian2D.h:222
 Cartesian2D.h:223
 Cartesian2D.h:224
 Cartesian2D.h:225
 Cartesian2D.h:226
 Cartesian2D.h:227
 Cartesian2D.h:228
 Cartesian2D.h:229
 Cartesian2D.h:230
 Cartesian2D.h:231
 Cartesian2D.h:232
 Cartesian2D.h:233
 Cartesian2D.h:234
 Cartesian2D.h:235
 Cartesian2D.h:236
 Cartesian2D.h:237
 Cartesian2D.h:238
 Cartesian2D.h:239
 Cartesian2D.h:240
 Cartesian2D.h:241
 Cartesian2D.h:242
 Cartesian2D.h:243
 Cartesian2D.h:244
 Cartesian2D.h:245
 Cartesian2D.h:246
 Cartesian2D.h:247
 Cartesian2D.h:248
 Cartesian2D.h:249
 Cartesian2D.h:250
 Cartesian2D.h:251
 Cartesian2D.h:252
 Cartesian2D.h:253
 Cartesian2D.h:254
 Cartesian2D.h:255
 Cartesian2D.h:256