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