ROOT logo
// @(#)root/mathcore:$Id$
// Authors: W. Brown, M. Fischler, L. Moneta    2005  

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

// Header file for class Polar2D
// 
// Created by: Lorenzo Moneta  at Mon May 30 11:40:03 2005
// Major revamp:  M. Fischler  at Wed Jun  8 2005
// 
// Last update: $Id$
// 
#ifndef ROOT_Math_GenVector_Polar2D 
#define ROOT_Math_GenVector_Polar2D  1

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

#ifndef ROOT_Math_GenVector_etaMax
#include "Math/GenVector/etaMax.h"
#endif


 
namespace ROOT { 

namespace Math { 


//__________________________________________________________________________________________
   /** 
       Class describing a polar 2D coordinate system based on r and phi
       Phi is restricted to be in the range [-PI,PI)
          
       @ingroup GenVector
   */ 


template <class T> 
class Polar2D { 

public : 

   typedef T Scalar;

   /**
      Default constructor with r=1,phi=0
   */
   Polar2D() : fR(1.), fPhi(0) {  }

   /**
      Construct from the polar coordinates:  r and phi
   */
   Polar2D(T r,T phi) : fR(r), fPhi(phi) { Restrict(); }

   /**
      Construct from any Vector or coordinate system implementing 
      R() and Phi()
   */ 
   template <class CoordSystem > 
   explicit Polar2D( const CoordSystem & v ) : 
      fR(v.R() ),  fPhi(v.Phi() )  { Restrict(); } 

   // 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
    */
   Polar2D(const Polar2D & v) :
      fR(v.R() ),  fPhi(v.Phi() )  {   } 

   /**
      assignment operator 
    */
   Polar2D & operator= (const Polar2D & v) { 
      fR     = v.R();  
      fPhi   = v.Phi(); 
      return *this;
   } 


   /**
      Set internal data based on 2 Scalar numbers
   */ 
   void SetCoordinates(Scalar r, Scalar  phi) 
   { fR=r; fPhi=phi; Restrict(); }

   /**
      get internal data into 2 Scalar numbers
   */ 
   void GetCoordinates(Scalar& r, Scalar& phi) const {r=fR; phi=fPhi;}

   
   Scalar R()     const { return fR;}
   Scalar Phi()   const { return fPhi; }
   Scalar X()     const { return fR*std::cos(fPhi);}
   Scalar Y()     const { return fR*std::sin(fPhi);}
   Scalar Mag2()  const { return fR*fR;}


   // setters (only for data members) 

  
   /** 
       set the r coordinate value keeping phi constant
   */ 
   void SetR(const T & r) { 
      fR = r;      
   }


   /** 
       set the phi coordinate value keeping r constant
   */ 
   void SetPhi(const T & phi) { 
      fPhi = phi;      
      Restrict();
   }

   /** 
       set all values using cartesian coordinates  
   */
   void SetXY(Scalar a, Scalar b); 


private:
   inline static double pi()  { return M_PI; } 

   /**
      restrict abgle hi to be between -PI and PI
    */
   inline void Restrict() {
      if ( fPhi <= -pi() || fPhi > pi() ) 
         fPhi = fPhi - std::floor( fPhi/(2*pi()) +.5 ) * 2*pi();
      return;
   } 

public:   

   /** 
       scale by a scalar quantity - for polar coordinates r changes
   */
   void Scale (T a) { 
      if (a < 0) {
         Negate();
         a = -a;
      }
      // angles do not change when scaling by a positive quantity
      fR *= a;     
   }

   /**
      negate the vector
   */ 
   void Negate ( ) { 
      fPhi = ( fPhi > 0 ? fPhi - pi() : fPhi + pi() );
   }

   /**
      rotate the vector 
    */
   void Rotate(T angle) { 
      fPhi += angle; 
      Restrict();
   }

   // assignment operators
   /**
      generic assignment operator from any coordinate system 
   */ 
   template <class CoordSystem > 
   Polar2D & operator= ( const CoordSystem & c ) { 
      fR     = c.R();  
      fPhi   = c.Phi(); 
      return *this;
   } 

   /**
      Exact equality
   */  
   bool operator==(const Polar2D & rhs) const {
      return fR == rhs.fR && fPhi == rhs.fPhi;
   }
   bool operator!= (const Polar2D & 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
   T x() const { return X();}
   T y() const { return Y();}
  
   // ============= Specializations for improved speed ==================

   // (none)

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

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

   void SetX(Scalar a);

   void SetY(Scalar a); 

#endif

private:
   T fR;        
   T fPhi;
};


   } // end namespace Math

} // end namespace ROOT


// move implementations here to avoid circle dependencies

#ifndef ROOT_Math_GenVector_Cartesian2D
#include "Math/GenVector/Cartesian2D.h"
#endif

#if defined(__MAKECINT__) || defined(G__DICTIONARY) 
#ifndef ROOT_Math_GenVector_GenVector_exception 
#include "Math/GenVector/GenVector_exception.h"
#endif
#endif

namespace ROOT { 

   namespace Math { 

template <class T>  
void Polar2D<T>::SetXY(Scalar a, Scalar b) {  
   *this = Cartesian2D<Scalar>(a, b);
}


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


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

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

#endif  


   } // end namespace Math

} // end namespace ROOT



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