CMS 3D CMS Logo

angle_units.h
Go to the documentation of this file.
1 #ifndef DataFormats_Math_Angle_Units_h
2 #define DataFormats_Math_Angle_Units_h
3 
4 #include <cmath>
5 
6 namespace angle_units {
7 
8  constexpr double piRadians(M_PI);
9  constexpr double degPerRad = 180. / piRadians; // Degrees per radian
10 
11  namespace operators {
12 
13  // Angle
14  constexpr double operator"" _pi(long double x) { return double(x) * M_PI; }
15  constexpr double operator"" _pi(unsigned long long int x) { return double(x) * M_PI; }
16  constexpr double operator"" _deg(long double deg) { return deg / degPerRad; }
17  constexpr double operator"" _deg(unsigned long long int deg) { return deg / degPerRad; }
18  constexpr double operator"" _rad(long double rad) { return rad * 1.; }
19 
20  template <class NumType>
21  inline constexpr NumType convertRadToDeg(NumType radians) // Radians -> degrees
22  {
23  return (radians * degPerRad);
24  }
25 
26  template <class NumType>
27  inline constexpr double convertDegToRad(NumType degrees) // Degrees -> radians
28  {
29  return (degrees / degPerRad);
30  }
31 
32  template <class NumType>
33  typename std::enable_if<!std::numeric_limits<NumType>::is_integer, bool>::type almostEqual(NumType x,
34  NumType y,
35  int ulp) {
36  return std::fabs(x - y) <= std::numeric_limits<NumType>::epsilon() * std::fabs(x + y) * ulp ||
37  std::fabs(x - y) < std::numeric_limits<NumType>::min();
38  }
39 
40  // Unit conversion functions. They are not related to angles, but it is convenient to define them here
41  // to avoid code duplication.
42 
43  template <class NumType>
44  inline constexpr NumType convertMmToCm(NumType millimeters) // Millimeters -> centimeters
45  {
46  return (millimeters / 10.);
47  }
48 
49  template <class NumType>
50  inline constexpr NumType convertCmToMm(NumType centimeters) // Centimeters -> Milliimeters
51  {
52  return (centimeters * 10.);
53  }
54 
55  template <class NumType>
56  inline constexpr NumType convertCm2ToMm2(NumType centimeters) // Centimeters^2 -> Milliimeters^2
57  {
58  return (centimeters * 100.);
59  }
60 
61  template <class NumType>
62  inline constexpr NumType convertMm3ToM3(NumType mm3) // Cubic millimeters -> cubic meters
63  {
64  return (mm3 / 1.e9);
65  }
66 
67  template <class NumType>
68  inline constexpr NumType convertMeVToGeV(NumType mev) // MeV -> GeV
69  {
70  return (mev * 0.001);
71  }
72 
73  template <class NumType>
74  inline constexpr NumType convertGeVToMeV(NumType gev) // GeV -> MeV
75  {
76  return (gev * 1000.);
77  }
78 
79  template <class NumType>
80  inline constexpr NumType convertGeVToKeV(NumType gev) // GeV -> keV
81  {
82  return (gev * 1.e6);
83  }
84 
85  } // namespace operators
86 } // namespace angle_units
87 
88 #endif
double degrees(double radiants)
def degrees
constexpr NumType convertCm2ToMm2(NumType centimeters)
Definition: angle_units.h:56
constexpr double degPerRad
Definition: angle_units.h:9
constexpr double convertDegToRad(NumType degrees)
Definition: angle_units.h:27
constexpr NumType convertCmToMm(NumType centimeters)
Definition: angle_units.h:50
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
std::enable_if<!std::numeric_limits< NumType >::is_integer, bool >::type almostEqual(NumType x, NumType y, int ulp)
Definition: angle_units.h:33
constexpr NumType convertMeVToGeV(NumType mev)
Definition: angle_units.h:68
constexpr NumType convertGeVToMeV(NumType gev)
Definition: angle_units.h:74
constexpr NumType convertMm3ToM3(NumType mm3)
Definition: angle_units.h:62
#define M_PI
constexpr NumType convertGeVToKeV(NumType gev)
Definition: angle_units.h:80
constexpr NumType convertMmToCm(NumType millimeters)
Definition: angle_units.h:44
constexpr double piRadians(M_PI)
float x