Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:40

0001 #ifndef DataFormats_Math_Angle_Units_h
0002 #define DataFormats_Math_Angle_Units_h
0003 
0004 #include <cmath>
0005 
0006 namespace angle_units {
0007 
0008   constexpr double piRadians(M_PI);
0009   constexpr double degPerRad = 180. / piRadians;  // Degrees per radian
0010 
0011   namespace operators {
0012 
0013     // Angle
0014     constexpr double operator"" _pi(long double x) { return double(x) * M_PI; }
0015     constexpr double operator"" _pi(unsigned long long int x) { return double(x) * M_PI; }
0016     constexpr double operator"" _deg(long double deg) { return deg / degPerRad; }
0017     constexpr double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }
0018     constexpr double operator"" _rad(long double rad) { return rad * 1.; }
0019 
0020     template <class NumType>
0021     inline constexpr NumType convertRadToDeg(NumType radians)  // Radians -> degrees
0022     {
0023       return (radians * degPerRad);
0024     }
0025 
0026     template <class NumType>
0027     inline constexpr double convertDegToRad(NumType degrees)  // Degrees -> radians
0028     {
0029       return (degrees / degPerRad);
0030     }
0031 
0032     template <class NumType>
0033     typename std::enable_if<!std::numeric_limits<NumType>::is_integer, bool>::type almostEqual(NumType x,
0034                                                                                                NumType y,
0035                                                                                                int ulp) {
0036       return std::fabs(x - y) <= std::numeric_limits<NumType>::epsilon() * std::fabs(x + y) * ulp ||
0037              std::fabs(x - y) < std::numeric_limits<NumType>::min();
0038     }
0039 
0040     // Unit conversion functions. They are not related to angles, but it is convenient to define them here
0041     // to avoid code duplication.
0042 
0043     template <class NumType>
0044     inline constexpr NumType convertMmToCm(NumType millimeters)  // Millimeters -> centimeters
0045     {
0046       return (millimeters / 10.);
0047     }
0048 
0049     template <class NumType>
0050     inline constexpr NumType convertCmToMm(NumType centimeters)  // Centimeters -> Milliimeters
0051     {
0052       return (centimeters * 10.);
0053     }
0054 
0055     template <class NumType>
0056     inline constexpr NumType convertCm2ToMm2(NumType centimeters)  // Centimeters^2 -> Milliimeters^2
0057     {
0058       return (centimeters * 100.);
0059     }
0060 
0061     template <class NumType>
0062     inline constexpr NumType convertMm3ToM3(NumType mm3)  // Cubic millimeters -> cubic meters
0063     {
0064       return (mm3 / 1.e9);
0065     }
0066 
0067     template <class NumType>
0068     inline constexpr NumType convertMeVToGeV(NumType mev)  // MeV -> GeV
0069     {
0070       return (mev * 0.001);
0071     }
0072 
0073     template <class NumType>
0074     inline constexpr NumType convertGeVToMeV(NumType gev)  // GeV -> MeV
0075     {
0076       return (gev * 1000.);
0077     }
0078 
0079     template <class NumType>
0080     inline constexpr NumType convertGeVToKeV(NumType gev)  // GeV -> keV
0081     {
0082       return (gev * 1.e6);
0083     }
0084 
0085   }  // namespace operators
0086 }  // namespace angle_units
0087 
0088 #endif