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
Area.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_AREA_HPP
26#define PHQ_UNIT_AREA_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 Area units.
53enum class Area : int8_t {
54 /// \brief Square metre (m^2) area unit.
56
57 /// \brief Square nautical mile (nmi^2) area unit.
59
60 /// \brief Square mile (mi^2) area unit.
62
63 /// \brief Square kilometre (km^2) area unit.
65
66 /// \brief Hectare (ha) area unit.
67 Hectare,
68
69 /// \brief Acre (ac) area unit.
70 Acre,
71
72 /// \brief Square yard (yd^2) area unit.
74
75 /// \brief Square foot (ft^2) area unit.
77
78 /// \brief Square decimetre (dm^2) area unit.
80
81 /// \brief Square inch (in^2) area unit.
83
84 /// \brief Square centimetre (cm^2) area unit.
86
87 /// \brief Square millimetre (mm^2) area unit.
89
90 /// \brief Square milliinch (mil^2) area unit.
92
93 /// \brief Square micrometre (μm^2) area unit.
95
96 /// \brief Square microinch (μin^2) area unit.
98};
99
100} // namespace Unit
101
102/// \brief Standard area unit: square metre (m^2).
103template <>
104inline constexpr const Unit::Area Standard<Unit::Area>{Unit::Area::SquareMetre};
105
106/// \brief Physical dimension set of area units.
107template <>
108inline constexpr const Dimensions RelatedDimensions<Unit::Area>{
109 Dimensions{Dimension::Time{0}, Dimension::Length{2}, 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::Area unit) {
115 stream << Abbreviation(unit);
116 return stream;
117}
118
119namespace Internal {
120
121template <>
122inline const std::map<UnitSystem, Unit::Area> ConsistentUnits<Unit::Area>{
127};
128
129template <>
130inline const std::map<Unit::Area, UnitSystem> RelatedUnitSystems<Unit::Area>{
135};
136
137// clang-format off
138
139template <>
140inline const std::map<Unit::Area, std::string_view> Abbreviations<Unit::Area>{
141 {Unit::Area::SquareMetre, "m^2" },
143 {Unit::Area::SquareMile, "mi^2" },
145 {Unit::Area::Hectare, "ha" },
146 {Unit::Area::Acre, "ac" },
147 {Unit::Area::SquareYard, "yd^2" },
148 {Unit::Area::SquareFoot, "ft^2" },
150 {Unit::Area::SquareInch, "in^2" },
155 {Unit::Area::SquareMicroinch, "μin^2"},
156};
157
158template <>
159inline const std::unordered_map<std::string_view, Unit::Area> Spellings<Unit::Area>{
160 {"m^2", Unit::Area::SquareMetre },
166 {"mi^2", Unit::Area::SquareMile },
167 {"mi2", Unit::Area::SquareMile },
170 {"ha", Unit::Area::Hectare },
171 {"ac", Unit::Area::Acre },
172 {"yd^2", Unit::Area::SquareYard },
173 {"yd2", Unit::Area::SquareYard },
174 {"ft^2", Unit::Area::SquareFoot },
175 {"ft2", Unit::Area::SquareFoot },
178 {"in^2", Unit::Area::SquareInch },
179 {"in2", Unit::Area::SquareInch },
184 {"millinch^2", Unit::Area::SquareMilliinch },
185 {"millinch2", Unit::Area::SquareMilliinch },
186 {"milliinch^2", Unit::Area::SquareMilliinch },
187 {"milliinch2", Unit::Area::SquareMilliinch },
188 {"mil^2", Unit::Area::SquareMilliinch },
190 {"thou^2", Unit::Area::SquareMilliinch },
191 {"thou2", Unit::Area::SquareMilliinch },
196 {"μin^2", Unit::Area::SquareMicroinch },
197 {"μin2", Unit::Area::SquareMicroinch },
198 {"uin^2", Unit::Area::SquareMicroinch },
200};
201
202// clang-format on
203
204template <>
205template <typename NumericType>
206inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMetre>::FromStandard(
207 NumericType& /*value*/) noexcept {}
208
209template <>
210template <typename NumericType>
211inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMetre>::ToStandard(
212 NumericType& /*value*/) noexcept {}
213
214template <>
215template <typename NumericType>
216inline constexpr void Conversion<Unit::Area, Unit::Area::SquareNauticalMile>::FromStandard(
217 NumericType& value) noexcept {
218 value /= static_cast<NumericType>(1852.0L) * static_cast<NumericType>(1852.0L);
219}
220
221template <>
222template <typename NumericType>
223inline constexpr void Conversion<Unit::Area, Unit::Area::SquareNauticalMile>::ToStandard(
224 NumericType& value) noexcept {
225 value *= static_cast<NumericType>(1852.0L) * static_cast<NumericType>(1852.0L);
226}
227
228template <>
229template <typename NumericType>
230inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMile>::FromStandard(
231 NumericType& value) noexcept {
232 value /= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L);
233}
234
235template <>
236template <typename NumericType>
237inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMile>::ToStandard(
238 NumericType& value) noexcept {
239 value *= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L);
240}
241
242template <>
243template <typename NumericType>
244inline constexpr void Conversion<Unit::Area, Unit::Area::SquareKilometre>::FromStandard(
245 NumericType& value) noexcept {
246 value *= static_cast<NumericType>(0.000001L);
247}
248
249template <>
250template <typename NumericType>
251inline constexpr void Conversion<Unit::Area, Unit::Area::SquareKilometre>::ToStandard(
252 NumericType& value) noexcept {
253 value *= static_cast<NumericType>(1000000.0L);
254}
255
256template <>
257template <typename NumericType>
258inline constexpr void Conversion<Unit::Area, Unit::Area::Hectare>::FromStandard(
259 NumericType& value) noexcept {
260 value *= static_cast<NumericType>(0.0001L);
261}
262
263template <>
264template <typename NumericType>
265inline constexpr void Conversion<Unit::Area, Unit::Area::Hectare>::ToStandard(
266 NumericType& value) noexcept {
267 value *= static_cast<NumericType>(10000.0L);
268}
269
270template <>
271template <typename NumericType>
272inline constexpr void Conversion<Unit::Area, Unit::Area::Acre>::FromStandard(
273 NumericType& value) noexcept {
274 value *= static_cast<NumericType>(640.0L)
275 / (static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L));
276}
277
278template <>
279template <typename NumericType>
280inline constexpr void Conversion<Unit::Area, Unit::Area::Acre>::ToStandard(
281 NumericType& value) noexcept {
282 value *= static_cast<NumericType>(1609.344L) * static_cast<NumericType>(1609.344L)
283 / static_cast<NumericType>(640.0L);
284}
285
286template <>
287template <typename NumericType>
288inline constexpr void Conversion<Unit::Area, Unit::Area::SquareYard>::FromStandard(
289 NumericType& value) noexcept {
290 value /= static_cast<NumericType>(0.9144L) * static_cast<NumericType>(0.9144L);
291}
292
293template <>
294template <typename NumericType>
295inline constexpr void Conversion<Unit::Area, Unit::Area::SquareYard>::ToStandard(
296 NumericType& value) noexcept {
297 value *= static_cast<NumericType>(0.9144L) * static_cast<NumericType>(0.9144L);
298}
299
300template <>
301template <typename NumericType>
302inline constexpr void Conversion<Unit::Area, Unit::Area::SquareFoot>::FromStandard(
303 NumericType& value) noexcept {
304 value /= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L);
305}
306
307template <>
308template <typename NumericType>
309inline constexpr void Conversion<Unit::Area, Unit::Area::SquareFoot>::ToStandard(
310 NumericType& value) noexcept {
311 value *= static_cast<NumericType>(0.3048L) * static_cast<NumericType>(0.3048L);
312}
313
314template <>
315template <typename NumericType>
316inline constexpr void Conversion<Unit::Area, Unit::Area::SquareDecimetre>::FromStandard(
317 NumericType& value) noexcept {
318 value *= static_cast<NumericType>(100.0L);
319}
320
321template <>
322template <typename NumericType>
323inline constexpr void Conversion<Unit::Area, Unit::Area::SquareDecimetre>::ToStandard(
324 NumericType& value) noexcept {
325 value *= static_cast<NumericType>(0.01L);
326}
327
328template <>
329template <typename NumericType>
330inline constexpr void Conversion<Unit::Area, Unit::Area::SquareInch>::FromStandard(
331 NumericType& value) noexcept {
332 value /= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L);
333}
334
335template <>
336template <typename NumericType>
337inline constexpr void Conversion<Unit::Area, Unit::Area::SquareInch>::ToStandard(
338 NumericType& value) noexcept {
339 value *= static_cast<NumericType>(0.0254L) * static_cast<NumericType>(0.0254L);
340}
341
342template <>
343template <typename NumericType>
344inline constexpr void Conversion<Unit::Area, Unit::Area::SquareCentimetre>::FromStandard(
345 NumericType& value) noexcept {
346 value *= static_cast<NumericType>(10000.0L);
347}
348
349template <>
350template <typename NumericType>
351inline constexpr void Conversion<Unit::Area, Unit::Area::SquareCentimetre>::ToStandard(
352 NumericType& value) noexcept {
353 value *= static_cast<NumericType>(0.0001L);
354}
355
356template <>
357template <typename NumericType>
358inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMillimetre>::FromStandard(
359 NumericType& value) noexcept {
360 value *= static_cast<NumericType>(1000000.0L);
361}
362
363template <>
364template <typename NumericType>
365inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMillimetre>::ToStandard(
366 NumericType& value) noexcept {
367 value *= static_cast<NumericType>(0.000001L);
368}
369
370template <>
371template <typename NumericType>
372inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMilliinch>::FromStandard(
373 NumericType& value) noexcept {
374 value /= static_cast<NumericType>(0.0000254L) * static_cast<NumericType>(0.0000254L);
375}
376
377template <>
378template <typename NumericType>
379inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMilliinch>::ToStandard(
380 NumericType& value) noexcept {
381 value *= static_cast<NumericType>(0.0000254L) * static_cast<NumericType>(0.0000254L);
382}
383
384template <>
385template <typename NumericType>
386inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMicrometre>::FromStandard(
387 NumericType& value) noexcept {
388 value *= static_cast<NumericType>(1000000000000.0L);
389}
390
391template <>
392template <typename NumericType>
393inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMicrometre>::ToStandard(
394 NumericType& value) noexcept {
395 value *= static_cast<NumericType>(0.000000000001L);
396}
397
398template <>
399template <typename NumericType>
400inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMicroinch>::FromStandard(
401 NumericType& value) noexcept {
402 value /= static_cast<NumericType>(0.0000000254L) * static_cast<NumericType>(0.0000000254L);
403}
404
405template <>
406template <typename NumericType>
407inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMicroinch>::ToStandard(
408 NumericType& value) noexcept {
409 value *= static_cast<NumericType>(0.0000000254L) * static_cast<NumericType>(0.0000000254L);
410}
411
412template <typename NumericType>
413inline const std::map<Unit::Area, std::function<void(NumericType* const, const std::size_t size)>>
414 MapOfConversionsFromStandard<Unit::Area, NumericType>{
416 Conversions<Unit::Area, Unit::Area::SquareMetre>::FromStandard<NumericType> },
418 Conversions<Unit::Area, Unit::Area::SquareNauticalMile>::FromStandard<NumericType>},
420 Conversions<Unit::Area, Unit::Area::SquareMile>::FromStandard<NumericType> },
422 Conversions<Unit::Area, Unit::Area::SquareKilometre>::FromStandard<NumericType> },
424 Conversions<Unit::Area, Unit::Area::Hectare>::FromStandard<NumericType> },
425 {Unit::Area::Acre, Conversions<Unit::Area, Unit::Area::Acre>::FromStandard<NumericType> },
427 Conversions<Unit::Area, Unit::Area::SquareYard>::FromStandard<NumericType> },
429 Conversions<Unit::Area, Unit::Area::SquareFoot>::FromStandard<NumericType> },
431 Conversions<Unit::Area, Unit::Area::SquareDecimetre>::FromStandard<NumericType> },
433 Conversions<Unit::Area, Unit::Area::SquareInch>::FromStandard<NumericType> },
435 Conversions<Unit::Area, Unit::Area::SquareCentimetre>::FromStandard<NumericType> },
437 Conversions<Unit::Area, Unit::Area::SquareMillimetre>::FromStandard<NumericType> },
439 Conversions<Unit::Area, Unit::Area::SquareMilliinch>::FromStandard<NumericType> },
441 Conversions<Unit::Area, Unit::Area::SquareMicrometre>::FromStandard<NumericType> },
443 Conversions<Unit::Area, Unit::Area::SquareMicroinch>::FromStandard<NumericType> },
444};
445
446template <typename NumericType>
447inline const std::map<Unit::Area, std::function<void(NumericType* values, const std::size_t size)>>
448 MapOfConversionsToStandard<Unit::Area, NumericType>{
450 Conversions<Unit::Area, Unit::Area::SquareMetre>::ToStandard<NumericType> },
452 Conversions<Unit::Area, Unit::Area::SquareNauticalMile>::ToStandard<NumericType>},
454 Conversions<Unit::Area, Unit::Area::SquareMile>::ToStandard<NumericType> },
456 Conversions<Unit::Area, Unit::Area::SquareKilometre>::ToStandard<NumericType> },
457 {Unit::Area::Hectare, Conversions<Unit::Area, Unit::Area::Hectare>::ToStandard<NumericType> },
458 {Unit::Area::Acre, Conversions<Unit::Area, Unit::Area::Acre>::ToStandard<NumericType> },
460 Conversions<Unit::Area, Unit::Area::SquareYard>::ToStandard<NumericType> },
462 Conversions<Unit::Area, Unit::Area::SquareFoot>::ToStandard<NumericType> },
464 Conversions<Unit::Area, Unit::Area::SquareDecimetre>::ToStandard<NumericType> },
466 Conversions<Unit::Area, Unit::Area::SquareInch>::ToStandard<NumericType> },
468 Conversions<Unit::Area, Unit::Area::SquareCentimetre>::ToStandard<NumericType> },
470 Conversions<Unit::Area, Unit::Area::SquareMillimetre>::ToStandard<NumericType> },
472 Conversions<Unit::Area, Unit::Area::SquareMilliinch>::ToStandard<NumericType> },
474 Conversions<Unit::Area, Unit::Area::SquareMicrometre>::ToStandard<NumericType> },
476 Conversions<Unit::Area, Unit::Area::SquareMicroinch>::ToStandard<NumericType> },
477};
478
479} // namespace Internal
480
481} // namespace PhQ
482
483#endif // PHQ_UNIT_AREA_HPP
Area
Area units.
Definition Area.hpp:53
@ SquareYard
Square yard (yd^2) area unit.
@ Acre
Acre (ac) area unit.
@ SquareKilometre
Square kilometre (km^2) area unit.
@ SquareMicrometre
Square micrometre (μm^2) area unit.
@ SquareNauticalMile
Square nautical mile (nmi^2) area unit.
@ SquareMilliinch
Square milliinch (mil^2) area unit.
@ Hectare
Hectare (ha) area unit.
@ SquareCentimetre
Square centimetre (cm^2) area unit.
@ SquareMile
Square mile (mi^2) area unit.
@ SquareMicroinch
Square microinch (μin^2) area unit.
@ SquareFoot
Square foot (ft^2) area unit.
@ SquareMillimetre
Square millimetre (mm^2) area unit.
@ SquareInch
Square inch (in^2) area unit.
@ SquareDecimetre
Square decimetre (dm^2) area unit.
@ SquareMetre
Square metre (m^2) area 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