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
Volume.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_VOLUME_HPP
26#define PHQ_UNIT_VOLUME_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 Volume units.
53enum class Volume : int8_t {
54 /// \brief Cubic metre (m^3) volume unit.
56
57 /// \brief Cubic nautical mile (nmi^3) volume unit.
59
60 /// \brief Cubic mile (mi^3) volume unit.
62
63 /// \brief Cubic kilometre (km^3) volume unit.
65
66 /// \brief Cubic yard (yd^3) volume unit.
68
69 /// \brief Cubic foot (ft^3) volume unit.
71
72 /// \brief Cubic decimetre (dm^3) volume unit.
74
75 /// \brief Litre (L) volume unit.
76 Litre,
77
78 /// \brief Cubic inch (in^3) volume unit.
80
81 /// \brief Cubic centimetre (cm^3) volume unit.
83
84 /// \brief Millilitre (mL) volume unit.
86
87 /// \brief Cubic millimetre (mm^3) volume unit.
89
90 /// \brief Cubic milliinch (mil^3) volume unit.
92
93 /// \brief Cubic micrometre (μm^3) volume unit.
95
96 /// \brief Cubic microinch (μin^3) volume unit.
98};
99
100} // namespace Unit
101
102/// \brief Standard volume unit: cubic metre (m^3).
103template <>
104inline constexpr const Unit::Volume Standard<Unit::Volume>{Unit::Volume::CubicMetre};
105
106/// \brief Physical dimension set of volume units.
107template <>
108inline constexpr const Dimensions RelatedDimensions<Unit::Volume>{
109 Dimensions{Dimension::Time{0}, Dimension::Length{3}, Dimension::Mass{0},
110 Dimension::ElectricCurrent{0}, Dimension::Temperature{0},
111 Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
112};
113
114inline std::ostream& operator<<(std::ostream& stream, const Unit::Volume unit) {
115 stream << Abbreviation(unit);
116 return stream;
117}
118
119namespace Internal {
120
121template <>
122inline const std::map<UnitSystem, Unit::Volume> ConsistentUnits<Unit::Volume>{
127};
128
129template <>
130inline const std::map<Unit::Volume, UnitSystem> RelatedUnitSystems<Unit::Volume>{
135};
136
137// clang-format off
138
139template <>
140inline const std::map<Unit::Volume, std::string_view> Abbreviations<Unit::Volume>{
141 {Unit::Volume::CubicMetre, "m^3" },
143 {Unit::Volume::CubicMile, "mi^3" },
145 {Unit::Volume::CubicYard, "yd^3" },
146 {Unit::Volume::CubicFoot, "ft^3" },
148 {Unit::Volume::Litre, "L" },
149 {Unit::Volume::CubicInch, "in^3" },
156};
157
158template <>
159inline const std::unordered_map<std::string_view, Unit::Volume> Spellings<Unit::Volume>{
160 {"m^3", Unit::Volume::CubicMetre },
164 {"mi^3", Unit::Volume::CubicMile },
165 {"mi3", Unit::Volume::CubicMile },
168 {"yd^3", Unit::Volume::CubicYard },
169 {"yd3", Unit::Volume::CubicYard },
170 {"ft^3", Unit::Volume::CubicFoot },
171 {"ft3", Unit::Volume::CubicFoot },
174 {"L", Unit::Volume::Litre },
175 {"in^3", Unit::Volume::CubicInch },
176 {"in3", Unit::Volume::CubicInch },
182 {"millinch^3", Unit::Volume::CubicMilliinch },
183 {"millinch3", Unit::Volume::CubicMilliinch },
184 {"milliinch^3", Unit::Volume::CubicMilliinch },
185 {"milliinch3", Unit::Volume::CubicMilliinch },
188 {"thou^3", Unit::Volume::CubicMilliinch },
194 {"μin^3", Unit::Volume::CubicMicroinch },
198};
199
200// clang-format on
201
202template <>
203template <typename NumericType>
204inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMetre>::FromStandard(
205 NumericType& /*value*/) noexcept {}
206
207template <>
208template <typename NumericType>
209inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMetre>::ToStandard(
210 NumericType& /*value*/) noexcept {}
211
212template <>
213template <typename NumericType>
214inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicNauticalMile>::FromStandard(
215 NumericType& value) noexcept {
216 value /= static_cast<NumericType>(1852.0L) * static_cast<NumericType>(1852.0L)
217 * static_cast<NumericType>(1852.0L);
218}
219
220template <>
221template <typename NumericType>
222inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicNauticalMile>::ToStandard(
223 NumericType& value) noexcept {
224 value *= static_cast<NumericType>(1852.0L) * static_cast<NumericType>(1852.0L)
225 * static_cast<NumericType>(1852.0L);
226}
227
228template <>
229template <typename NumericType>
230inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMile>::FromStandard(
231 NumericType& value) noexcept {
232 value /= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L)
233 * static_cast<NumericType>(1609.344L);
234}
235
236template <>
237template <typename NumericType>
238inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMile>::ToStandard(
239 NumericType& value) noexcept {
240 value *= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L)
241 * static_cast<NumericType>(1609.344L);
242}
243
244template <>
245template <typename NumericType>
246inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicKilometre>::FromStandard(
247 NumericType& value) noexcept {
248 value *= static_cast<NumericType>(1.0E-9L);
249}
250
251template <>
252template <typename NumericType>
253inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicKilometre>::ToStandard(
254 NumericType& value) noexcept {
255 value *= static_cast<NumericType>(1.0E9L);
256}
257
258template <>
259template <typename NumericType>
260inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicYard>::FromStandard(
261 NumericType& value) noexcept {
262 value /= static_cast<NumericType>(0.9144L) * static_cast<NumericType>(0.9144L)
263 * static_cast<NumericType>(0.9144L);
264}
265
266template <>
267template <typename NumericType>
268inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicYard>::ToStandard(
269 NumericType& value) noexcept {
270 value *= static_cast<NumericType>(0.9144L) * static_cast<NumericType>(0.9144L)
271 * static_cast<NumericType>(0.9144L);
272}
273
274template <>
275template <typename NumericType>
276inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicFoot>::FromStandard(
277 NumericType& value) noexcept {
278 value /= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
279 * static_cast<NumericType>(0.3048L);
280}
281
282template <>
283template <typename NumericType>
284inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicFoot>::ToStandard(
285 NumericType& value) noexcept {
286 value *= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L)
287 * static_cast<NumericType>(0.3048L);
288}
289
290template <>
291template <typename NumericType>
292inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicDecimetre>::FromStandard(
293 NumericType& value) noexcept {
294 value *= static_cast<NumericType>(1000.0L);
295}
296
297template <>
298template <typename NumericType>
299inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicDecimetre>::ToStandard(
300 NumericType& value) noexcept {
301 value *= static_cast<NumericType>(0.001L);
302}
303
304template <>
305template <typename NumericType>
306inline constexpr void Conversion<Unit::Volume, Unit::Volume::Litre>::FromStandard(
307 NumericType& value) noexcept {
308 value *= static_cast<NumericType>(1000.0L);
309}
310
311template <>
312template <typename NumericType>
313inline constexpr void Conversion<Unit::Volume, Unit::Volume::Litre>::ToStandard(
314 NumericType& value) noexcept {
315 value *= static_cast<NumericType>(0.001L);
316}
317
318template <>
319template <typename NumericType>
320inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicInch>::FromStandard(
321 NumericType& value) noexcept {
322 value /= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
323 * static_cast<NumericType>(0.0254L);
324}
325
326template <>
327template <typename NumericType>
328inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicInch>::ToStandard(
329 NumericType& value) noexcept {
330 value *= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L)
331 * static_cast<NumericType>(0.0254L);
332}
333
334template <>
335template <typename NumericType>
336inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicCentimetre>::FromStandard(
337 NumericType& value) noexcept {
338 value *= static_cast<NumericType>(1.0E6L);
339}
340
341template <>
342template <typename NumericType>
343inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicCentimetre>::ToStandard(
344 NumericType& value) noexcept {
345 value *= static_cast<NumericType>(1.0E-6L);
346}
347
348template <>
349template <typename NumericType>
350inline constexpr void Conversion<Unit::Volume, Unit::Volume::Millilitre>::FromStandard(
351 NumericType& value) noexcept {
352 value *= static_cast<NumericType>(1.0E6L);
353}
354
355template <>
356template <typename NumericType>
357inline constexpr void Conversion<Unit::Volume, Unit::Volume::Millilitre>::ToStandard(
358 NumericType& value) noexcept {
359 value *= static_cast<NumericType>(1.0E-6L);
360}
361
362template <>
363template <typename NumericType>
364inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMillimetre>::FromStandard(
365 NumericType& value) noexcept {
366 value *= static_cast<NumericType>(1.0E9L);
367}
368
369template <>
370template <typename NumericType>
371inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMillimetre>::ToStandard(
372 NumericType& value) noexcept {
373 value *= static_cast<NumericType>(1.0E-9L);
374}
375
376template <>
377template <typename NumericType>
378inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMilliinch>::FromStandard(
379 NumericType& value) noexcept {
380 value /= static_cast<NumericType>(0.0000254L) * static_cast<NumericType>(0.0000254L)
381 * static_cast<NumericType>(0.0000254L);
382}
383
384template <>
385template <typename NumericType>
386inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMilliinch>::ToStandard(
387 NumericType& value) noexcept {
388 value *= static_cast<NumericType>(0.0000254L) * static_cast<NumericType>(0.0000254L)
389 * static_cast<NumericType>(0.0000254L);
390}
391
392template <>
393template <typename NumericType>
394inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicrometre>::FromStandard(
395 NumericType& value) noexcept {
396 value *= static_cast<NumericType>(1.0E18L);
397}
398
399template <>
400template <typename NumericType>
401inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicrometre>::ToStandard(
402 NumericType& value) noexcept {
403 value *= static_cast<NumericType>(1.0E-18L);
404}
405
406template <>
407template <typename NumericType>
408inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicroinch>::FromStandard(
409 NumericType& value) noexcept {
410 value /= static_cast<NumericType>(0.0000000254L) * static_cast<NumericType>(0.0000000254L)
411 * static_cast<NumericType>(0.0000000254L);
412}
413
414template <>
415template <typename NumericType>
416inline constexpr void Conversion<Unit::Volume, Unit::Volume::CubicMicroinch>::ToStandard(
417 NumericType& value) noexcept {
418 value *= static_cast<NumericType>(0.0000000254L) * static_cast<NumericType>(0.0000000254L)
419 * static_cast<NumericType>(0.0000000254L);
420}
421
422template <typename NumericType>
423inline const std::map<Unit::Volume, std::function<void(NumericType* const, const std::size_t size)>>
424 MapOfConversionsFromStandard<Unit::Volume, NumericType>{
426 Conversions<Unit::Volume, Unit::Volume::CubicMetre>::FromStandard<NumericType> },
428 Conversions<Unit::Volume, Unit::Volume::CubicNauticalMile>::FromStandard<NumericType>},
430 Conversions<Unit::Volume, Unit::Volume::CubicMile>::FromStandard<NumericType> },
432 Conversions<Unit::Volume, Unit::Volume::CubicKilometre>::FromStandard<NumericType> },
434 Conversions<Unit::Volume, Unit::Volume::CubicYard>::FromStandard<NumericType> },
436 Conversions<Unit::Volume, Unit::Volume::CubicFoot>::FromStandard<NumericType> },
438 Conversions<Unit::Volume, Unit::Volume::CubicDecimetre>::FromStandard<NumericType> },
440 Conversions<Unit::Volume, Unit::Volume::Litre>::FromStandard<NumericType> },
442 Conversions<Unit::Volume, Unit::Volume::CubicInch>::FromStandard<NumericType> },
444 Conversions<Unit::Volume, Unit::Volume::CubicCentimetre>::FromStandard<NumericType> },
446 Conversions<Unit::Volume, Unit::Volume::Millilitre>::FromStandard<NumericType> },
448 Conversions<Unit::Volume, Unit::Volume::CubicMillimetre>::FromStandard<NumericType> },
450 Conversions<Unit::Volume, Unit::Volume::CubicMilliinch>::FromStandard<NumericType> },
452 Conversions<Unit::Volume, Unit::Volume::CubicMicrometre>::FromStandard<NumericType> },
454 Conversions<Unit::Volume, Unit::Volume::CubicMicroinch>::FromStandard<NumericType> },
455};
456
457template <typename NumericType>
458inline const std::
459 map<Unit::Volume, std::function<void(NumericType* values, const std::size_t size)>>
460 MapOfConversionsToStandard<Unit::Volume, NumericType>{
462 Conversions<Unit::Volume, Unit::Volume::CubicMetre>::ToStandard<NumericType> },
464 Conversions<Unit::Volume, Unit::Volume::CubicNauticalMile>::ToStandard<NumericType>},
466 Conversions<Unit::Volume, Unit::Volume::CubicMile>::ToStandard<NumericType> },
468 Conversions<Unit::Volume, Unit::Volume::CubicKilometre>::ToStandard<NumericType> },
470 Conversions<Unit::Volume, Unit::Volume::CubicYard>::ToStandard<NumericType> },
472 Conversions<Unit::Volume, Unit::Volume::CubicFoot>::ToStandard<NumericType> },
474 Conversions<Unit::Volume, Unit::Volume::CubicDecimetre>::ToStandard<NumericType> },
476 Conversions<Unit::Volume, Unit::Volume::Litre>::ToStandard<NumericType> },
478 Conversions<Unit::Volume, Unit::Volume::CubicInch>::ToStandard<NumericType> },
480 Conversions<Unit::Volume, Unit::Volume::CubicCentimetre>::ToStandard<NumericType> },
482 Conversions<Unit::Volume, Unit::Volume::Millilitre>::ToStandard<NumericType> },
484 Conversions<Unit::Volume, Unit::Volume::CubicMillimetre>::ToStandard<NumericType> },
486 Conversions<Unit::Volume, Unit::Volume::CubicMilliinch>::ToStandard<NumericType> },
488 Conversions<Unit::Volume, Unit::Volume::CubicMicrometre>::ToStandard<NumericType> },
490 Conversions<Unit::Volume, Unit::Volume::CubicMicroinch>::ToStandard<NumericType> },
491};
492
493} // namespace Internal
494
495} // namespace PhQ
496
497#endif // PHQ_UNIT_VOLUME_HPP
Volume
Volume units.
Definition Volume.hpp:53
@ CubicKilometre
Cubic kilometre (km^3) volume unit.
@ Litre
Litre (L) volume unit.
@ CubicNauticalMile
Cubic nautical mile (nmi^3) volume unit.
@ CubicMicrometre
Cubic micrometre (μm^3) volume unit.
@ CubicYard
Cubic yard (yd^3) volume unit.
@ Millilitre
Millilitre (mL) volume unit.
@ CubicFoot
Cubic foot (ft^3) volume unit.
@ CubicDecimetre
Cubic decimetre (dm^3) volume unit.
@ CubicCentimetre
Cubic centimetre (cm^3) volume unit.
@ CubicMicroinch
Cubic microinch (μin^3) volume unit.
@ CubicInch
Cubic inch (in^3) volume unit.
@ CubicMilliinch
Cubic milliinch (mil^3) volume unit.
@ CubicMetre
Cubic metre (m^3) volume unit.
@ CubicMillimetre
Cubic millimetre (mm^3) volume unit.
@ CubicMile
Cubic mile (mi^3) volume 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