Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Length.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_LENGTH_HPP
26 #define PHQ_UNIT_LENGTH_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 Length units.
53 enum class Length : int8_t {
54  /// \brief Metre (m) length unit.
55  Metre,
56 
57  /// \brief Nautical mile (nmi) length unit.
59 
60  /// \brief Mile (mi) length unit.
61  Mile,
62 
63  /// \brief Kilometre (km) length unit.
64  Kilometre,
65 
66  /// \brief Yard (yd) length unit.
67  Yard,
68 
69  /// \brief Foot (ft) length unit.
70  Foot,
71 
72  /// \brief Decimetre (dm) length unit.
73  Decimetre,
74 
75  /// \brief Inch (in) length unit.
76  Inch,
77 
78  /// \brief Centimetre (cm) length unit.
79  Centimetre,
80 
81  /// \brief Millimetre (mm) length unit.
82  Millimetre,
83 
84  /// \brief Milliinch (mil) length unit.
85  Milliinch,
86 
87  /// \brief Micrometre (μm) length unit.
88  Micrometre,
89 
90  /// \brief Microinch (μin) length unit.
91  Microinch,
92 };
93 
94 } // namespace Unit
95 
96 /// \brief Standard length unit: metre (m).
97 template <>
98 inline constexpr const Unit::Length Standard<Unit::Length>{Unit::Length::Metre};
99 
100 /// \brief Physical dimension set of length units.
101 template <>
102 inline constexpr const Dimensions RelatedDimensions<Unit::Length>{
103  Dimensions{Dimension::Time{0}, Dimension::Length{1}, Dimension::Mass{0},
105  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
106 };
107 
108 inline std::ostream& operator<<(std::ostream& stream, const Unit::Length unit) {
109  stream << Abbreviation(unit);
110  return stream;
111 }
112 
113 namespace Internal {
114 
115 template <>
116 inline const std::map<UnitSystem, Unit::Length> ConsistentUnits<Unit::Length>{
121 };
122 
123 template <>
124 inline const std::map<Unit::Length, UnitSystem> RelatedUnitSystems<Unit::Length>{
129 };
130 
131 // clang-format off
132 
133 template <>
134 inline const std::map<Unit::Length, std::string_view> Abbreviations<Unit::Length>{
135  {Unit::Length::Metre, "m" },
137  {Unit::Length::Mile, "mi" },
138  {Unit::Length::Kilometre, "km" },
139  {Unit::Length::Yard, "yd" },
140  {Unit::Length::Foot, "ft" },
141  {Unit::Length::Decimetre, "dm" },
142  {Unit::Length::Inch, "in" },
143  {Unit::Length::Centimetre, "cm" },
144  {Unit::Length::Millimetre, "mm" },
145  {Unit::Length::Milliinch, "mil"},
146  {Unit::Length::Micrometre, "μm" },
147  {Unit::Length::Microinch, "μin"},
148 };
149 
150 template <>
151 inline const std::unordered_map<std::string_view, Unit::Length> Spellings<Unit::Length>{
152  {"m", Unit::Length::Metre },
153  {"meter", Unit::Length::Metre },
154  {"meters", Unit::Length::Metre },
155  {"metre", Unit::Length::Metre },
156  {"metres", Unit::Length::Metre },
159  {"nautical mile", Unit::Length::NauticalMile},
160  {"nautical miles", Unit::Length::NauticalMile},
161  {"mi", Unit::Length::Mile },
162  {"mile", Unit::Length::Mile },
163  {"miles", Unit::Length::Mile },
164  {"km", Unit::Length::Kilometre },
165  {"kilometer", Unit::Length::Kilometre },
166  {"kilometers", Unit::Length::Kilometre },
167  {"kilometre", Unit::Length::Kilometre },
168  {"kilometres", Unit::Length::Kilometre },
169  {"yd", Unit::Length::Yard },
170  {"yard", Unit::Length::Yard },
171  {"yards", Unit::Length::Yard },
172  {"ft", Unit::Length::Foot },
173  {"foot", Unit::Length::Foot },
174  {"feet", Unit::Length::Foot },
175  {"dm", Unit::Length::Decimetre },
176  {"decimeter", Unit::Length::Decimetre },
177  {"decimeters", Unit::Length::Decimetre },
178  {"decimetre", Unit::Length::Decimetre },
179  {"decimetres", Unit::Length::Decimetre },
180  {"in", Unit::Length::Inch },
181  {"inch", Unit::Length::Inch },
182  {"inches", Unit::Length::Inch },
183  {"cm", Unit::Length::Centimetre },
184  {"centimeter", Unit::Length::Centimetre },
185  {"centimeters", Unit::Length::Centimetre },
186  {"centimetre", Unit::Length::Centimetre },
187  {"centimetres", Unit::Length::Centimetre },
188  {"mm", Unit::Length::Millimetre },
189  {"millimeter", Unit::Length::Millimetre },
190  {"millimeters", Unit::Length::Millimetre },
191  {"millimetre", Unit::Length::Millimetre },
192  {"millimetres", Unit::Length::Millimetre },
193  {"milin", Unit::Length::Milliinch },
194  {"milliinch", Unit::Length::Milliinch },
195  {"milliinches", Unit::Length::Milliinch },
196  {"mil", Unit::Length::Milliinch },
197  {"mils", Unit::Length::Milliinch },
198  {"thou", Unit::Length::Milliinch },
199  {"thous", Unit::Length::Milliinch },
200  {"thousandth", Unit::Length::Milliinch },
201  {"thousandths", Unit::Length::Milliinch },
202  {"μm", Unit::Length::Micrometre },
203  {"um", Unit::Length::Micrometre },
204  {"micrometer", Unit::Length::Micrometre },
205  {"micrometers", Unit::Length::Micrometre },
206  {"Micrometre", Unit::Length::Micrometre },
207  {"Micrometres", Unit::Length::Micrometre },
208  {"micron", Unit::Length::Micrometre },
209  {"microns", Unit::Length::Micrometre },
210  {"μin", Unit::Length::Microinch },
211  {"uin", Unit::Length::Microinch },
212  {"microinch", Unit::Length::Microinch },
213  {"microinches", Unit::Length::Microinch },
214 };
215 
216 // clang-format on
217 
218 template <>
219 template <typename NumericType>
220 inline constexpr void Conversion<Unit::Length, Unit::Length::Metre>::FromStandard(
221  NumericType& /*value*/) noexcept {}
222 
223 template <>
224 template <typename NumericType>
225 inline constexpr void Conversion<Unit::Length, Unit::Length::Metre>::ToStandard(
226  NumericType& /*value*/) noexcept {}
227 
228 template <>
229 template <typename NumericType>
230 inline constexpr void Conversion<Unit::Length, Unit::Length::NauticalMile>::FromStandard(
231  NumericType& value) noexcept {
232  value /= static_cast<NumericType>(1852.0L);
233 }
234 
235 template <>
236 template <typename NumericType>
237 inline constexpr void Conversion<Unit::Length, Unit::Length::NauticalMile>::ToStandard(
238  NumericType& value) noexcept {
239  value *= static_cast<NumericType>(1852.0L);
240 }
241 
242 template <>
243 template <typename NumericType>
244 inline constexpr void Conversion<Unit::Length, Unit::Length::Mile>::FromStandard(
245  NumericType& value) noexcept {
246  value /= static_cast<NumericType>(1609.344L);
247 }
248 
249 template <>
250 template <typename NumericType>
251 inline constexpr void Conversion<Unit::Length, Unit::Length::Mile>::ToStandard(
252  NumericType& value) noexcept {
253  value *= static_cast<NumericType>(1609.344L);
254 }
255 
256 template <>
257 template <typename NumericType>
258 inline constexpr void Conversion<Unit::Length, Unit::Length::Kilometre>::FromStandard(
259  NumericType& value) noexcept {
260  value *= static_cast<NumericType>(0.001L);
261 }
262 
263 template <>
264 template <typename NumericType>
265 inline constexpr void Conversion<Unit::Length, Unit::Length::Kilometre>::ToStandard(
266  NumericType& value) noexcept {
267  value *= static_cast<NumericType>(1000.0L);
268 }
269 
270 template <>
271 template <typename NumericType>
272 inline constexpr void Conversion<Unit::Length, Unit::Length::Yard>::FromStandard(
273  NumericType& value) noexcept {
274  value /= static_cast<NumericType>(0.9144L);
275 }
276 
277 template <>
278 template <typename NumericType>
279 inline constexpr void Conversion<Unit::Length, Unit::Length::Yard>::ToStandard(
280  NumericType& value) noexcept {
281  value *= static_cast<NumericType>(0.9144L);
282 }
283 
284 template <>
285 template <typename NumericType>
286 inline constexpr void Conversion<Unit::Length, Unit::Length::Foot>::FromStandard(
287  NumericType& value) noexcept {
288  value /= static_cast<NumericType>(0.3048L);
289 }
290 
291 template <>
292 template <typename NumericType>
293 inline constexpr void Conversion<Unit::Length, Unit::Length::Foot>::ToStandard(
294  NumericType& value) noexcept {
295  value *= static_cast<NumericType>(0.3048L);
296 }
297 
298 template <>
299 template <typename NumericType>
300 inline constexpr void Conversion<Unit::Length, Unit::Length::Decimetre>::FromStandard(
301  NumericType& value) noexcept {
302  value *= static_cast<NumericType>(10.0L);
303 }
304 
305 template <>
306 template <typename NumericType>
307 inline constexpr void Conversion<Unit::Length, Unit::Length::Decimetre>::ToStandard(
308  NumericType& value) noexcept {
309  value *= static_cast<NumericType>(0.1L);
310 }
311 
312 template <>
313 template <typename NumericType>
314 inline constexpr void Conversion<Unit::Length, Unit::Length::Inch>::FromStandard(
315  NumericType& value) noexcept {
316  value /= static_cast<NumericType>(0.0254L);
317 }
318 
319 template <>
320 template <typename NumericType>
321 inline constexpr void Conversion<Unit::Length, Unit::Length::Inch>::ToStandard(
322  NumericType& value) noexcept {
323  value *= static_cast<NumericType>(0.0254L);
324 }
325 
326 template <>
327 template <typename NumericType>
328 inline constexpr void Conversion<Unit::Length, Unit::Length::Centimetre>::FromStandard(
329  NumericType& value) noexcept {
330  value *= static_cast<NumericType>(100.0L);
331 }
332 
333 template <>
334 template <typename NumericType>
335 inline constexpr void Conversion<Unit::Length, Unit::Length::Centimetre>::ToStandard(
336  NumericType& value) noexcept {
337  value *= static_cast<NumericType>(0.01L);
338 }
339 
340 template <>
341 template <typename NumericType>
342 inline constexpr void Conversion<Unit::Length, Unit::Length::Millimetre>::FromStandard(
343  NumericType& value) noexcept {
344  value *= static_cast<NumericType>(1000.0L);
345 }
346 
347 template <>
348 template <typename NumericType>
349 inline constexpr void Conversion<Unit::Length, Unit::Length::Millimetre>::ToStandard(
350  NumericType& value) noexcept {
351  value *= static_cast<NumericType>(0.001L);
352 }
353 
354 template <>
355 template <typename NumericType>
356 inline constexpr void Conversion<Unit::Length, Unit::Length::Milliinch>::FromStandard(
357  NumericType& value) noexcept {
358  value /= static_cast<NumericType>(0.0000254L);
359 }
360 
361 template <>
362 template <typename NumericType>
363 inline constexpr void Conversion<Unit::Length, Unit::Length::Milliinch>::ToStandard(
364  NumericType& value) noexcept {
365  value *= static_cast<NumericType>(0.0000254L);
366 }
367 
368 template <>
369 template <typename NumericType>
370 inline constexpr void Conversion<Unit::Length, Unit::Length::Micrometre>::FromStandard(
371  NumericType& value) noexcept {
372  value *= static_cast<NumericType>(1000000.0L);
373 }
374 
375 template <>
376 template <typename NumericType>
377 inline constexpr void Conversion<Unit::Length, Unit::Length::Micrometre>::ToStandard(
378  NumericType& value) noexcept {
379  value *= static_cast<NumericType>(0.000001L);
380 }
381 
382 template <>
383 template <typename NumericType>
384 inline constexpr void Conversion<Unit::Length, Unit::Length::Microinch>::FromStandard(
385  NumericType& value) noexcept {
386  value /= static_cast<NumericType>(0.0000000254L);
387 }
388 
389 template <>
390 template <typename NumericType>
391 inline constexpr void Conversion<Unit::Length, Unit::Length::Microinch>::ToStandard(
392  NumericType& value) noexcept {
393  value *= static_cast<NumericType>(0.0000000254L);
394 }
395 
396 template <typename NumericType>
397 inline const std::map<Unit::Length,
398  std::function<void(NumericType* values, const std::size_t size)>>
399  MapOfConversionsFromStandard<Unit::Length, NumericType>{
401  Conversions<Unit::Length, Unit::Length::Metre>::FromStandard<NumericType> },
403  Conversions<Unit::Length, Unit::Length::NauticalMile>::FromStandard<NumericType>},
405  Conversions<Unit::Length, Unit::Length::Mile>::FromStandard<NumericType> },
407  Conversions<Unit::Length, Unit::Length::Kilometre>::FromStandard<NumericType> },
409  Conversions<Unit::Length, Unit::Length::Yard>::FromStandard<NumericType> },
411  Conversions<Unit::Length, Unit::Length::Foot>::FromStandard<NumericType> },
413  Conversions<Unit::Length, Unit::Length::Decimetre>::FromStandard<NumericType> },
415  Conversions<Unit::Length, Unit::Length::Inch>::FromStandard<NumericType> },
417  Conversions<Unit::Length, Unit::Length::Centimetre>::FromStandard<NumericType> },
419  Conversions<Unit::Length, Unit::Length::Millimetre>::FromStandard<NumericType> },
421  Conversions<Unit::Length, Unit::Length::Milliinch>::FromStandard<NumericType> },
423  Conversions<Unit::Length, Unit::Length::Micrometre>::FromStandard<NumericType> },
425  Conversions<Unit::Length, Unit::Length::Microinch>::FromStandard<NumericType> },
426 };
427 
428 template <typename NumericType>
429 inline const std::map<Unit::Length,
430  std::function<void(NumericType* const values, const std::size_t size)>>
431  MapOfConversionsToStandard<Unit::Length, NumericType>{
433  Conversions<Unit::Length, Unit::Length::Metre>::ToStandard<NumericType> },
435  Conversions<Unit::Length, Unit::Length::NauticalMile>::ToStandard<NumericType>},
437  Conversions<Unit::Length, Unit::Length::Mile>::ToStandard<NumericType> },
439  Conversions<Unit::Length, Unit::Length::Kilometre>::ToStandard<NumericType> },
441  Conversions<Unit::Length, Unit::Length::Yard>::ToStandard<NumericType> },
443  Conversions<Unit::Length, Unit::Length::Foot>::ToStandard<NumericType> },
445  Conversions<Unit::Length, Unit::Length::Decimetre>::ToStandard<NumericType> },
447  Conversions<Unit::Length, Unit::Length::Inch>::ToStandard<NumericType> },
449  Conversions<Unit::Length, Unit::Length::Centimetre>::ToStandard<NumericType> },
451  Conversions<Unit::Length, Unit::Length::Millimetre>::ToStandard<NumericType> },
453  Conversions<Unit::Length, Unit::Length::Milliinch>::ToStandard<NumericType> },
455  Conversions<Unit::Length, Unit::Length::Micrometre>::ToStandard<NumericType> },
457  Conversions<Unit::Length, Unit::Length::Microinch>::ToStandard<NumericType> },
458 };
459 
460 } // namespace Internal
461 
462 } // namespace PhQ
463 
464 #endif // PHQ_UNIT_LENGTH_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
@ Centimetre
Centimetre (cm) length unit.
@ Metre
Metre (m) length unit.
@ Foot
Foot (ft) length unit.
@ NauticalMile
Nautical mile (nmi) length unit.
@ Milliinch
Milliinch (mil) length unit.
@ Yard
Yard (yd) length unit.
@ Mile
Mile (mi) length unit.
@ Decimetre
Decimetre (dm) length unit.
@ Inch
Inch (in) length unit.
@ Millimetre
Millimetre (mm) length unit.
@ Micrometre
Micrometre (μm) length unit.
@ Kilometre
Kilometre (km) length unit.
@ Microinch
Microinch (μin) length unit.
SubstanceAmount
Amount of substance units.
Temperature
Temperature units. Not to be confused with temperature difference units. For example,...
Definition: Temperature.hpp:55
Time
Time units.
Definition: Time.hpp:53
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.
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)