CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/DataFormats/GeometrySurface/interface/GloballyPositioned.h

Go to the documentation of this file.
00001 #ifndef Geom_GloballyPositioned_H
00002 #define Geom_GloballyPositioned_H
00003 
00004 #include "DataFormats/GeometryVector/interface/Point3DBase.h"
00005 #include "DataFormats/GeometryVector/interface/Vector3DBase.h"
00006 #include "DataFormats/GeometryVector/interface/LocalTag.h"
00007 #include "DataFormats/GeometryVector/interface/GlobalTag.h"
00008 #include "DataFormats/GeometrySurface/interface/TkRotation.h"
00009 
00017 template <class T>
00018 class GloballyPositioned {
00019 public:
00020 
00021   typedef T                             Scalar;
00022   typedef Point3DBase<T,GlobalTag>      PositionType;
00023   typedef TkRotation<T>                 RotationType;
00024   typedef Point3DBase<T,GlobalTag>      GlobalPoint;
00025   typedef Point3DBase<T,LocalTag>       LocalPoint;
00026   typedef Vector3DBase<T,GlobalTag>     GlobalVector;
00027   typedef Vector3DBase<T,LocalTag>      LocalVector;
00028 
00029   static T iniPhi() {
00030     return 999.9978;
00031   }
00032   static T iniEta() {
00033     return 999.9978;
00034   }
00035 
00036   GloballyPositioned(){}
00037   GloballyPositioned( const PositionType& pos, const RotationType& rot) :
00038     thePos(pos), theRot(rot) {resetCache();}
00039 
00040   virtual ~GloballyPositioned() {}
00041 
00042   const PositionType& position() const { return thePos;}
00043 
00044   const RotationType& rotation() const { return theRot;}
00045 
00046   T phi() const {
00047     if (thePhi==iniPhi()) thePhi = thePos.barePhi();
00048     return thePhi;
00049   }
00050   T eta() const { 
00051     if (theEta==iniEta()) theEta = thePos.eta();
00052     return theEta;
00053   }
00054 
00055 
00056   // multiply inverse is faster
00057   class ToLocal {
00058   public:
00059     ToLocal(GloballyPositioned const & frame) :
00060       thePos(frame.position()), theRot(frame.rotation().transposed()){}
00061     
00062     LocalPoint toLocal( const GlobalPoint& gp) const {
00063       return LocalPoint( theRot.multiplyInverse( gp.basicVector() -
00064                          thePos.basicVector()) 
00065                        );
00066     }
00067     
00068     LocalVector toLocal( const GlobalVector& gv) const {
00069       return LocalVector(theRot.multiplyInverse(gv.basicVector()));
00070     } 
00071     
00072   private:
00073     PositionType  thePos;
00074     RotationType  theRot;
00075     
00076   };
00077 
00078   
00079 
00083   GlobalPoint toGlobal( const LocalPoint& lp) const {
00084     return GlobalPoint( rotation().multiplyInverse( lp.basicVector()) +
00085                         position().basicVector());
00086   }
00087 
00092   template <class U>
00093   Point3DBase< U, GlobalTag>
00094   toGlobal( const Point3DBase< U, LocalTag>& lp) const {
00095     return Point3DBase< U, GlobalTag>( rotation().multiplyInverse( lp.basicVector()) +
00096                                        position().basicVector());
00097   }
00098 
00102   GlobalVector toGlobal( const LocalVector& lv) const {
00103     return GlobalVector( rotation().multiplyInverse( lv.basicVector()));
00104   }
00105 
00110   template <class U>
00111   Vector3DBase< U, GlobalTag>
00112   toGlobal( const Vector3DBase< U, LocalTag>& lv) const {
00113     return Vector3DBase< U, GlobalTag>( rotation().multiplyInverse( lv.basicVector()));
00114   }
00115 
00119   LocalPoint toLocal( const GlobalPoint& gp) const {
00120     return LocalPoint( rotation() * (gp.basicVector()-position().basicVector()));
00121   }
00122 
00127   template <class U>
00128   Point3DBase< U, LocalTag>
00129   toLocal( const Point3DBase< U, GlobalTag>& gp) const {
00130     return Point3DBase< U, LocalTag>( rotation() * 
00131                                       (gp.basicVector()-position().basicVector()));
00132   }
00133 
00137   LocalVector toLocal( const GlobalVector& gv) const {
00138     return LocalVector( rotation() * gv.basicVector());
00139   }
00140 
00145   template <class U>
00146   Vector3DBase< U, LocalTag>
00147   toLocal( const Vector3DBase< U, GlobalTag>& gv) const {
00148     return Vector3DBase< U, LocalTag>( rotation() * gv.basicVector());
00149   }
00150 
00154   void move( const GlobalVector& displacement) {
00155     thePos += displacement;
00156     resetCache();
00157   }
00158 
00162   void rotate( const RotationType& rotation) {
00163     theRot *= rotation;
00164     resetCache();
00165   }
00166 
00167 private:
00168 
00169   PositionType  thePos;
00170   RotationType  theRot;
00171 
00172 
00173   void resetCache() {
00174     if ((thePos.x() == 0.) && (thePos.y() == 0.)) {
00175       thePhi = theEta = 0.; // avoid FPE
00176     } else {
00177       thePhi = iniPhi();
00178       theEta = iniEta();
00179     }
00180   }
00181 
00182 
00183   void setCache() {
00184     if ((thePos.x() == 0.) && (thePos.y() == 0.)) {
00185       thePhi = theEta = 0.; // avoid FPE
00186     } else {
00187       thePhi = thePos.barePhi();
00188       theEta = thePos.eta();
00189     }
00190   }
00191   
00192   mutable T thePhi;
00193   mutable T theEta;
00194 
00195 };
00196   
00197 #endif