Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
Loading...
Searching...
No Matches
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
48namespace PhQ {
49
50namespace Unit {
51
52/// \brief Length units.
53enum 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.
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.
74
75 /// \brief Inch (in) length unit.
76 Inch,
77
78 /// \brief Centimetre (cm) length unit.
80
81 /// \brief Millimetre (mm) length unit.
83
84 /// \brief Milliinch (mil) length unit.
86
87 /// \brief Micrometre (μm) length unit.
89
90 /// \brief Microinch (μin) length unit.
92};
93
94} // namespace Unit
95
96/// \brief Standard length unit: metre (m).
97template <>
98inline constexpr const Unit::Length Standard<Unit::Length>{Unit::Length::Metre};
99
100/// \brief Physical dimension set of length units.
101template <>
102inline constexpr const Dimensions RelatedDimensions<Unit::Length>{
103 Dimensions{Dimension::Time{0}, Dimension::Length{1}, Dimension::Mass{0},
104 Dimension::ElectricCurrent{0}, Dimension::Temperature{0},
105 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
106};
107
108inline std::ostream& operator<<(std::ostream& stream, const Unit::Length unit) {
109 stream << Abbreviation(unit);
110 return stream;
111}
112
113namespace Internal {
114
115template <>
116inline const std::map<UnitSystem, Unit::Length> ConsistentUnits<Unit::Length>{
121};
122
123template <>
124inline const std::map<Unit::Length, UnitSystem> RelatedUnitSystems<Unit::Length>{
129};
130
131// clang-format off
132
133template <>
134inline const std::map<Unit::Length, std::string_view> Abbreviations<Unit::Length>{
135 {Unit::Length::Metre, "m" },
137 {Unit::Length::Mile, "mi" },
139 {Unit::Length::Yard, "yd" },
140 {Unit::Length::Foot, "ft" },
142 {Unit::Length::Inch, "in" },
146 {Unit::Length::Micrometre, "μm" },
147 {Unit::Length::Microinch, "μin"},
148};
149
150template <>
151inline 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 },
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 },
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 },
184 {"centimeter", Unit::Length::Centimetre },
185 {"centimeters", Unit::Length::Centimetre },
186 {"centimetre", Unit::Length::Centimetre },
187 {"centimetres", Unit::Length::Centimetre },
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 },
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
218template <>
219template <typename NumericType>
220inline constexpr void Conversion<Unit::Length, Unit::Length::Metre>::FromStandard(
221 NumericType& /*value*/) noexcept {}
222
223template <>
224template <typename NumericType>
225inline constexpr void Conversion<Unit::Length, Unit::Length::Metre>::ToStandard(
226 NumericType& /*value*/) noexcept {}
227
228template <>
229template <typename NumericType>
230inline constexpr void Conversion<Unit::Length, Unit::Length::NauticalMile>::FromStandard(
231 NumericType& value) noexcept {
232 value /= static_cast<NumericType>(1852.0L);
233}
234
235template <>
236template <typename NumericType>
237inline constexpr void Conversion<Unit::Length, Unit::Length::NauticalMile>::ToStandard(
238 NumericType& value) noexcept {
239 value *= static_cast<NumericType>(1852.0L);
240}
241
242template <>
243template <typename NumericType>
244inline constexpr void Conversion<Unit::Length, Unit::Length::Mile>::FromStandard(
245 NumericType& value) noexcept {
246 value /= static_cast<NumericType>(1609.344L);
247}
248
249template <>
250template <typename NumericType>
251inline constexpr void Conversion<Unit::Length, Unit::Length::Mile>::ToStandard(
252 NumericType& value) noexcept {
253 value *= static_cast<NumericType>(1609.344L);
254}
255
256template <>
257template <typename NumericType>
258inline constexpr void Conversion<Unit::Length, Unit::Length::Kilometre>::FromStandard(
259 NumericType& value) noexcept {
260 value *= static_cast<NumericType>(0.001L);
261}
262
263template <>
264template <typename NumericType>
265inline constexpr void Conversion<Unit::Length, Unit::Length::Kilometre>::ToStandard(
266 NumericType& value) noexcept {
267 value *= static_cast<NumericType>(1000.0L);
268}
269
270template <>
271template <typename NumericType>
272inline constexpr void Conversion<Unit::Length, Unit::Length::Yard>::FromStandard(
273 NumericType& value) noexcept {
274 value /= static_cast<NumericType>(0.9144L);
275}
276
277template <>
278template <typename NumericType>
279inline constexpr void Conversion<Unit::Length, Unit::Length::Yard>::ToStandard(
280 NumericType& value) noexcept {
281 value *= static_cast<NumericType>(0.9144L);
282}
283
284template <>
285template <typename NumericType>
286inline constexpr void Conversion<Unit::Length, Unit::Length::Foot>::FromStandard(
287 NumericType& value) noexcept {
288 value /= static_cast<NumericType>(0.3048L);
289}
290
291template <>
292template <typename NumericType>
293inline constexpr void Conversion<Unit::Length, Unit::Length::Foot>::ToStandard(
294 NumericType& value) noexcept {
295 value *= static_cast<NumericType>(0.3048L);
296}
297
298template <>
299template <typename NumericType>
300inline constexpr void Conversion<Unit::Length, Unit::Length::Decimetre>::FromStandard(
301 NumericType& value) noexcept {
302 value *= static_cast<NumericType>(10.0L);
303}
304
305template <>
306template <typename NumericType>
307inline constexpr void Conversion<Unit::Length, Unit::Length::Decimetre>::ToStandard(
308 NumericType& value) noexcept {
309 value *= static_cast<NumericType>(0.1L);
310}
311
312template <>
313template <typename NumericType>
314inline constexpr void Conversion<Unit::Length, Unit::Length::Inch>::FromStandard(
315 NumericType& value) noexcept {
316 value /= static_cast<NumericType>(0.0254L);
317}
318
319template <>
320template <typename NumericType>
321inline constexpr void Conversion<Unit::Length, Unit::Length::Inch>::ToStandard(
322 NumericType& value) noexcept {
323 value *= static_cast<NumericType>(0.0254L);
324}
325
326template <>
327template <typename NumericType>
328inline constexpr void Conversion<Unit::Length, Unit::Length::Centimetre>::FromStandard(
329 NumericType& value) noexcept {
330 value *= static_cast<NumericType>(100.0L);
331}
332
333template <>
334template <typename NumericType>
335inline constexpr void Conversion<Unit::Length, Unit::Length::Centimetre>::ToStandard(
336 NumericType& value) noexcept {
337 value *= static_cast<NumericType>(0.01L);
338}
339
340template <>
341template <typename NumericType>
342inline constexpr void Conversion<Unit::Length, Unit::Length::Millimetre>::FromStandard(
343 NumericType& value) noexcept {
344 value *= static_cast<NumericType>(1000.0L);
345}
346
347template <>
348template <typename NumericType>
349inline constexpr void Conversion<Unit::Length, Unit::Length::Millimetre>::ToStandard(
350 NumericType& value) noexcept {
351 value *= static_cast<NumericType>(0.001L);
352}
353
354template <>
355template <typename NumericType>
356inline constexpr void Conversion<Unit::Length, Unit::Length::Milliinch>::FromStandard(
357 NumericType& value) noexcept {
358 value /= static_cast<NumericType>(0.0000254L);
359}
360
361template <>
362template <typename NumericType>
363inline constexpr void Conversion<Unit::Length, Unit::Length::Milliinch>::ToStandard(
364 NumericType& value) noexcept {
365 value *= static_cast<NumericType>(0.0000254L);
366}
367
368template <>
369template <typename NumericType>
370inline constexpr void Conversion<Unit::Length, Unit::Length::Micrometre>::FromStandard(
371 NumericType& value) noexcept {
372 value *= static_cast<NumericType>(1000000.0L);
373}
374
375template <>
376template <typename NumericType>
377inline constexpr void Conversion<Unit::Length, Unit::Length::Micrometre>::ToStandard(
378 NumericType& value) noexcept {
379 value *= static_cast<NumericType>(0.000001L);
380}
381
382template <>
383template <typename NumericType>
384inline constexpr void Conversion<Unit::Length, Unit::Length::Microinch>::FromStandard(
385 NumericType& value) noexcept {
386 value /= static_cast<NumericType>(0.0000000254L);
387}
388
389template <>
390template <typename NumericType>
391inline constexpr void Conversion<Unit::Length, Unit::Length::Microinch>::ToStandard(
392 NumericType& value) noexcept {
393 value *= static_cast<NumericType>(0.0000000254L);
394}
395
396template <typename NumericType>
397inline const std::
398 map<Unit::Length, 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
428template <typename NumericType>
429inline 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>},
436 {Unit::Length::Mile, Conversions<Unit::Length, Unit::Length::Mile>::ToStandard<NumericType> },
438 Conversions<Unit::Length, Unit::Length::Kilometre>::ToStandard<NumericType> },
439 {Unit::Length::Yard, Conversions<Unit::Length, Unit::Length::Yard>::ToStandard<NumericType> },
440 {Unit::Length::Foot, Conversions<Unit::Length, Unit::Length::Foot>::ToStandard<NumericType> },
442 Conversions<Unit::Length, Unit::Length::Decimetre>::ToStandard<NumericType> },
443 {Unit::Length::Inch, Conversions<Unit::Length, Unit::Length::Inch>::ToStandard<NumericType> },
445 Conversions<Unit::Length, Unit::Length::Centimetre>::ToStandard<NumericType> },
447 Conversions<Unit::Length, Unit::Length::Millimetre>::ToStandard<NumericType> },
449 Conversions<Unit::Length, Unit::Length::Milliinch>::ToStandard<NumericType> },
451 Conversions<Unit::Length, Unit::Length::Micrometre>::ToStandard<NumericType> },
453 Conversions<Unit::Length, Unit::Length::Microinch>::ToStandard<NumericType> },
454};
455
456} // namespace Internal
457
458} // namespace PhQ
459
460#endif // PHQ_UNIT_LENGTH_HPP
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.
Namespace that encompasses all of the Physical Quantities library's content.
std::ostream & operator<<(std::ostream &stream, const Acceleration< NumericType > &acceleration)
@ 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