Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:53:33

0001 #ifndef GeometryVector_Geom_CoordinateSets_h
0002 #define GeometryVector_Geom_CoordinateSets_h
0003 
0004 #include <cmath>
0005 
0006 namespace Geom {
0007 
0008   /** Converts polar 2D coordinates to cartesian coordinates.
0009  *  Note: Spherical coordinates (also sometimes called Polar 3D coordinates)
0010  *  are handled by class Spherical2Cartesian
0011  */
0012 
0013   template <typename T>
0014   class Polar2Cartesian {
0015   public:
0016     /// Construct from radius and polar angle
0017     Polar2Cartesian(const T& r, const T& phi) : r_(r), phi_(phi) {}
0018 
0019     const T& r() const { return r_; }
0020     const T& phi() const { return phi_; }
0021 
0022     T x() const { return r_ * cos(phi_); }
0023     T y() const { return r_ * sin(phi_); }
0024 
0025   private:
0026     T r_;
0027     T phi_;
0028   };
0029 
0030   /** Converts cylindtical coordinates to cartesian coordinates.
0031  */
0032 
0033   template <typename T>
0034   class Cylindrical2Cartesian {
0035   public:
0036     /** Construct from radius, azimuthal angle, and z component.
0037      *  The radius in the cylindrical frame is the transverse component.
0038      */
0039     Cylindrical2Cartesian(const T& r, const T& phi, const T& z) : r_(r), phi_(phi), z_(z) {}
0040 
0041     const T& r() const { return r_; }
0042     const T& phi() const { return phi_; }
0043     const T& z() const { return z_; }
0044 
0045     T x() const { return r_ * cos(phi_); }
0046     T y() const { return r_ * sin(phi_); }
0047 
0048   private:
0049     T r_;
0050     T phi_;
0051     T z_;
0052   };
0053 
0054   /** Converts spherical (or polar 3D) coordinates to cartesian coordinates.
0055  */
0056 
0057   template <typename T>
0058   class Spherical2Cartesian {
0059   public:
0060     /** Construct from polar angle, azimuthal angle, and radius.
0061      *  The radius in the spherical frame is the magnitude of the vector.
0062      */
0063     Spherical2Cartesian(const T& theta, const T& phi, const T& mag)
0064         : theta_(theta), phi_(phi), r_(mag), transv_(sin(theta) * mag) {}
0065 
0066     const T& theta() const { return theta_; }
0067     const T& phi() const { return phi_; }
0068     const T& r() const { return r_; }
0069 
0070     T x() const { return transv_ * cos(phi()); }
0071     T y() const { return transv_ * sin(phi()); }
0072     T z() const { return cos(theta()) * r(); }
0073 
0074   private:
0075     T theta_;
0076     T phi_;
0077     T r_;
0078     T transv_;
0079   };
0080 
0081   /** Cartesian coordinate set, for uniformity with other coordinate systems
0082  */
0083 
0084   template <typename T>
0085   class Cartesian2Cartesian3D {
0086   public:
0087     Cartesian2Cartesian3D(const T& x, const T& y, const T& z) : x_(x), y_(y), z_(z) {}
0088 
0089     const T& x() const { return x_; }
0090     const T& y() const { return y_; }
0091     const T& z() const { return z_; }
0092 
0093   private:
0094     T x_;
0095     T y_;
0096     T z_;
0097   };
0098 }  // namespace Geom
0099 
0100 #endif