Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Angle.hpp
Go to the documentation of this file.
1 // Copyright © 2020-2024 Alexandre Coderre-Chabot
2 //
3 // This file is part of Physical Quantities (PhQ), a C++ library of physical quantities, physical
4 // models, and units of measure for scientific computing.
5 //
6 // Physical Quantities is hosted at:
7 // https://github.com/acodcha/phq
8 //
9 // Physical Quantities is licensed under the MIT License:
10 // https://mit-license.org
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
13 // associated documentation files (the "Software"), to deal in the Software without restriction,
14 // including without limitation the rights to use, copy, modify, merge, publish, distribute,
15 // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 // - The above copyright notice and this permission notice shall be included in all copies or
18 // substantial portions of the Software.
19 // - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
20 // BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 #ifndef PHQ_UNIT_ANGLE_HPP
26 #define PHQ_UNIT_ANGLE_HPP
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <functional>
31 #include <map>
32 #include <ostream>
33 #include <string_view>
34 #include <unordered_map>
35 
36 #include "../Base.hpp"
37 #include "../Dimension/ElectricCurrent.hpp"
38 #include "../Dimension/Length.hpp"
39 #include "../Dimension/LuminousIntensity.hpp"
40 #include "../Dimension/Mass.hpp"
41 #include "../Dimension/SubstanceAmount.hpp"
42 #include "../Dimension/Temperature.hpp"
43 #include "../Dimension/Time.hpp"
44 #include "../Dimensions.hpp"
45 #include "../Unit.hpp"
46 #include "../UnitSystem.hpp"
47 
48 namespace PhQ {
49 
50 namespace Unit {
51 
52 /// \brief Angle units.
53 enum class Angle : int8_t {
54  /// \brief Radian (rad) angle unit.
55  Radian,
56 
57  /// \brief Degree (deg) angle unit.
58  Degree,
59 
60  /// \brief Arcminute (arcmin) angle unit.
61  Arcminute,
62 
63  /// \brief Arcsecond (arcsec) angle unit.
64  Arcsecond,
65 
66  /// \brief Revolution (rev) angle unit.
67  Revolution,
68 };
69 
70 } // namespace Unit
71 
72 /// \brief Standard angle unit: radian (rad).
73 template <>
74 inline constexpr const Unit::Angle Standard<Unit::Angle>{Unit::Angle::Radian};
75 
76 /// \brief Physical dimension set of angle units.
77 template <>
78 inline constexpr const Dimensions RelatedDimensions<Unit::Angle>{Dimensionless};
79 
80 inline std::ostream& operator<<(std::ostream& stream, const Unit::Angle unit) {
81  stream << Abbreviation(unit);
82  return stream;
83 }
84 
85 namespace Internal {
86 
87 template <>
88 inline const std::map<UnitSystem, Unit::Angle> ConsistentUnits<Unit::Angle>{
93 };
94 
95 template <>
96 inline const std::map<Unit::Angle, UnitSystem> RelatedUnitSystems<Unit::Angle>{};
97 
98 template <>
99 inline const std::map<Unit::Angle, std::string_view> Abbreviations<Unit::Angle>{
100  {Unit::Angle::Radian, "rad" },
101  {Unit::Angle::Degree, "deg" },
102  {Unit::Angle::Arcminute, "arcmin"},
103  {Unit::Angle::Arcsecond, "arcsec"},
104  {Unit::Angle::Revolution, "rev" },
105 };
106 
107 // clang-format off
108 
109 template <>
110 inline const std::unordered_map<std::string_view, Unit::Angle> Spellings<Unit::Angle>{
111  {"rad", Unit::Angle::Radian },
112  {"radian", Unit::Angle::Radian },
113  {"radians", Unit::Angle::Radian },
114  {"deg", Unit::Angle::Degree },
115  {"degree", Unit::Angle::Degree },
116  {"degrees", Unit::Angle::Degree },
117  {"°", Unit::Angle::Degree },
118  {"'", Unit::Angle::Arcminute },
119  {"am", Unit::Angle::Arcminute },
120  {"arcmin", Unit::Angle::Arcminute },
121  {"arcminute", Unit::Angle::Arcminute },
122  {"arcminutes", Unit::Angle::Arcminute },
123  {"\"", Unit::Angle::Arcsecond },
124  {"as", Unit::Angle::Arcsecond },
125  {"arcs", Unit::Angle::Arcsecond },
126  {"arcsec", Unit::Angle::Arcsecond },
127  {"arcsecond", Unit::Angle::Arcsecond },
128  {"arcseconds", Unit::Angle::Arcsecond },
129  {"rev", Unit::Angle::Revolution},
130  {"revolution", Unit::Angle::Revolution},
131  {"revolutions", Unit::Angle::Revolution},
132 };
133 
134 // clang-format on
135 
136 template <>
137 template <typename NumericType>
138 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Radian>::FromStandard(
139  NumericType& /*value*/) noexcept {}
140 
141 template <>
142 template <typename NumericType>
143 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Radian>::ToStandard(
144  NumericType& /*value*/) noexcept {}
145 
146 template <>
147 template <typename NumericType>
148 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Degree>::FromStandard(
149  NumericType& value) noexcept {
150  value *= static_cast<NumericType>(180.0L) / Pi<NumericType>;
151 }
152 
153 template <>
154 template <typename NumericType>
155 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Degree>::ToStandard(
156  NumericType& value) noexcept {
157  value *= Pi<NumericType> / static_cast<NumericType>(180.0L);
158 }
159 
160 template <>
161 template <typename NumericType>
162 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Arcminute>::FromStandard(
163  NumericType& value) noexcept {
164  value *= static_cast<NumericType>(10800.0L) / Pi<NumericType>;
165 }
166 
167 template <>
168 template <typename NumericType>
169 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Arcminute>::ToStandard(
170  NumericType& value) noexcept {
171  value *= Pi<NumericType> / static_cast<NumericType>(10800.0L);
172 }
173 
174 template <>
175 template <typename NumericType>
176 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Arcsecond>::FromStandard(
177  NumericType& value) noexcept {
178  value *= static_cast<NumericType>(648000.0L) / Pi<NumericType>;
179 }
180 
181 template <>
182 template <typename NumericType>
183 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Arcsecond>::ToStandard(
184  NumericType& value) noexcept {
185  value *= Pi<NumericType> / static_cast<NumericType>(648000.0L);
186 }
187 
188 template <>
189 template <typename NumericType>
190 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Revolution>::FromStandard(
191  NumericType& value) noexcept {
192  value *= static_cast<NumericType>(0.5L) / Pi<NumericType>;
193 }
194 
195 template <>
196 template <typename NumericType>
197 inline constexpr void Conversion<Unit::Angle, Unit::Angle::Revolution>::ToStandard(
198  NumericType& value) noexcept {
199  value *= static_cast<NumericType>(2.0L) * Pi<NumericType>;
200 }
201 
202 template <typename NumericType>
203 inline const std::map<Unit::Angle, std::function<void(NumericType* values, const std::size_t size)>>
204  MapOfConversionsFromStandard<Unit::Angle, NumericType>{
206  Conversions<Unit::Angle, Unit::Angle::Radian>::FromStandard<NumericType> },
208  Conversions<Unit::Angle, Unit::Angle::Degree>::FromStandard<NumericType> },
210  Conversions<Unit::Angle, Unit::Angle::Arcminute>::FromStandard<NumericType> },
212  Conversions<Unit::Angle, Unit::Angle::Arcsecond>::FromStandard<NumericType> },
214  Conversions<Unit::Angle, Unit::Angle::Revolution>::FromStandard<NumericType>},
215 };
216 
217 template <typename NumericType>
218 inline const std::map<Unit::Angle,
219  std::function<void(NumericType* const values, const std::size_t size)>>
220  MapOfConversionsToStandard<Unit::Angle, NumericType>{
222  Conversions<Unit::Angle, Unit::Angle::Radian>::ToStandard<NumericType> },
224  Conversions<Unit::Angle, Unit::Angle::Degree>::ToStandard<NumericType> },
226  Conversions<Unit::Angle, Unit::Angle::Arcminute>::ToStandard<NumericType> },
228  Conversions<Unit::Angle, Unit::Angle::Arcsecond>::ToStandard<NumericType> },
230  Conversions<Unit::Angle, Unit::Angle::Revolution>::ToStandard<NumericType>},
231 };
232 
233 } // namespace Internal
234 
235 } // namespace PhQ
236 
237 #endif // PHQ_UNIT_ANGLE_HPP
Angle
Angle units.
Definition: Angle.hpp:53
@ Radian
Radian (rad) angle unit.
@ Degree
Degree (deg) angle unit.
@ Arcminute
Arcminute (arcmin) angle unit.
@ Arcsecond
Arcsecond (arcsec) angle unit.
@ Revolution
Revolution (rev) angle unit.
Namespace that encompasses all of the Physical Quantities library's content.
@ FootPoundSecondRankine
Foot-pound-second-rankine (ft·lbf·s·°R) system.
@ MillimetreGramSecondKelvin
Millimetre-gram-second-kelvin (mm·g·s·K) system.
@ MetreKilogramSecondKelvin
Metre-kilogram-second-kelvin (m·kg·s·K) system.
@ InchPoundSecondRankine
Inch-pound-second-rankine (in·lbf·s·°R) system.
constexpr Dimensions Dimensionless
Dimensionless physical dimension set. This dimension set has all base dimensions of zero....
Definition: Dimensions.hpp:406
std::string_view Abbreviation(const Enumeration enumeration)
Returns the abbreviation of a given enumeration value. For example, PhQ::Abbreviation(PhQ::Unit::Time...
Definition: Base.hpp:89
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)