Physical Quantities  v1.0.0
C++ library of physical quantities, physical models, and units of measure for scientific computing. https://github.com/acodcha/phq
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 
48 namespace PhQ {
49 
50 namespace Unit {
51 
52 /// \brief Area units.
53 enum 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.
61  SquareMile,
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.
73  SquareYard,
74 
75  /// \brief Square foot (ft^2) area unit.
76  SquareFoot,
77 
78  /// \brief Square decimetre (dm^2) area unit.
80 
81  /// \brief Square inch (in^2) area unit.
82  SquareInch,
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).
103 template <>
104 inline constexpr const Unit::Area Standard<Unit::Area>{Unit::Area::SquareMetre};
105 
106 /// \brief Physical dimension set of area units.
107 template <>
108 inline constexpr const Dimensions RelatedDimensions<Unit::Area>{
109  Dimensions{Dimension::Time{0}, Dimension::Length{2}, Dimension::Mass{0},
111  Dimension::SubstanceAmount{0}, Dimension::LuminousIntensity{0}}
112 };
113 
114 inline std::ostream& operator<<(std::ostream& stream, const Unit::Area unit) {
115  stream << Abbreviation(unit);
116  return stream;
117 }
118 
119 namespace Internal {
120 
121 template <>
122 inline const std::map<UnitSystem, Unit::Area> ConsistentUnits<Unit::Area>{
127 };
128 
129 template <>
130 inline const std::map<Unit::Area, UnitSystem> RelatedUnitSystems<Unit::Area>{
135 };
136 
137 // clang-format off
138 
139 template <>
140 inline const std::map<Unit::Area, std::string_view> Abbreviations<Unit::Area>{
141  {Unit::Area::SquareMetre, "m^2" },
143  {Unit::Area::SquareMile, "mi^2" },
144  {Unit::Area::SquareKilometre, "km^2" },
145  {Unit::Area::Hectare, "ha" },
146  {Unit::Area::Acre, "ac" },
147  {Unit::Area::SquareYard, "yd^2" },
148  {Unit::Area::SquareFoot, "ft^2" },
149  {Unit::Area::SquareDecimetre, "dm^2" },
150  {Unit::Area::SquareInch, "in^2" },
151  {Unit::Area::SquareCentimetre, "cm^2" },
152  {Unit::Area::SquareMillimetre, "mm^2" },
153  {Unit::Area::SquareMilliinch, "mil^2"},
154  {Unit::Area::SquareMicrometre, "μm^2" },
155  {Unit::Area::SquareMicroinch, "μin^2"},
156 };
157 
158 template <>
159 inline const std::unordered_map<std::string_view, Unit::Area> Spellings<Unit::Area>{
160  {"m^2", Unit::Area::SquareMetre },
161  {"m2", Unit::Area::SquareMetre },
166  {"mi^2", Unit::Area::SquareMile },
167  {"mi2", Unit::Area::SquareMile },
168  {"km^2", Unit::Area::SquareKilometre },
169  {"km2", Unit::Area::SquareKilometre },
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 },
176  {"dm^2", Unit::Area::SquareDecimetre },
177  {"dm2", Unit::Area::SquareDecimetre },
178  {"in^2", Unit::Area::SquareInch },
179  {"in2", Unit::Area::SquareInch },
180  {"cm^2", Unit::Area::SquareCentimetre },
182  {"mm^2", Unit::Area::SquareMillimetre },
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 },
189  {"mil2", Unit::Area::SquareMilliinch },
190  {"thou^2", Unit::Area::SquareMilliinch },
191  {"thou2", Unit::Area::SquareMilliinch },
192  {"μm^2", Unit::Area::SquareMicrometre },
193  {"μm2", Unit::Area::SquareMicrometre },
194  {"um^2", Unit::Area::SquareMicrometre },
196  {"μin^2", Unit::Area::SquareMicroinch },
197  {"μin2", Unit::Area::SquareMicroinch },
198  {"uin^2", Unit::Area::SquareMicroinch },
199  {"uin2", Unit::Area::SquareMicroinch },
200 };
201 
202 // clang-format on
203 
204 template <>
205 template <typename NumericType>
206 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMetre>::FromStandard(
207  NumericType& /*value*/) noexcept {}
208 
209 template <>
210 template <typename NumericType>
211 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMetre>::ToStandard(
212  NumericType& /*value*/) noexcept {}
213 
214 template <>
215 template <typename NumericType>
216 inline 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 
221 template <>
222 template <typename NumericType>
223 inline 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 
228 template <>
229 template <typename NumericType>
230 inline 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 
235 template <>
236 template <typename NumericType>
237 inline 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 
242 template <>
243 template <typename NumericType>
244 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareKilometre>::FromStandard(
245  NumericType& value) noexcept {
246  value *= static_cast<NumericType>(0.000001L);
247 }
248 
249 template <>
250 template <typename NumericType>
251 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareKilometre>::ToStandard(
252  NumericType& value) noexcept {
253  value *= static_cast<NumericType>(1000000.0L);
254 }
255 
256 template <>
257 template <typename NumericType>
258 inline constexpr void Conversion<Unit::Area, Unit::Area::Hectare>::FromStandard(
259  NumericType& value) noexcept {
260  value *= static_cast<NumericType>(0.0001L);
261 }
262 
263 template <>
264 template <typename NumericType>
265 inline constexpr void Conversion<Unit::Area, Unit::Area::Hectare>::ToStandard(
266  NumericType& value) noexcept {
267  value *= static_cast<NumericType>(10000.0L);
268 }
269 
270 template <>
271 template <typename NumericType>
272 inline 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 
278 template <>
279 template <typename NumericType>
280 inline 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 
286 template <>
287 template <typename NumericType>
288 inline 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 
293 template <>
294 template <typename NumericType>
295 inline 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 
300 template <>
301 template <typename NumericType>
302 inline 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 
307 template <>
308 template <typename NumericType>
309 inline 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 
314 template <>
315 template <typename NumericType>
316 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareDecimetre>::FromStandard(
317  NumericType& value) noexcept {
318  value *= static_cast<NumericType>(100.0L);
319 }
320 
321 template <>
322 template <typename NumericType>
323 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareDecimetre>::ToStandard(
324  NumericType& value) noexcept {
325  value *= static_cast<NumericType>(0.01L);
326 }
327 
328 template <>
329 template <typename NumericType>
330 inline 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 
335 template <>
336 template <typename NumericType>
337 inline 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 
342 template <>
343 template <typename NumericType>
344 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareCentimetre>::FromStandard(
345  NumericType& value) noexcept {
346  value *= static_cast<NumericType>(10000.0L);
347 }
348 
349 template <>
350 template <typename NumericType>
351 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareCentimetre>::ToStandard(
352  NumericType& value) noexcept {
353  value *= static_cast<NumericType>(0.0001L);
354 }
355 
356 template <>
357 template <typename NumericType>
358 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMillimetre>::FromStandard(
359  NumericType& value) noexcept {
360  value *= static_cast<NumericType>(1000000.0L);
361 }
362 
363 template <>
364 template <typename NumericType>
365 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMillimetre>::ToStandard(
366  NumericType& value) noexcept {
367  value *= static_cast<NumericType>(0.000001L);
368 }
369 
370 template <>
371 template <typename NumericType>
372 inline 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 
377 template <>
378 template <typename NumericType>
379 inline 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 
384 template <>
385 template <typename NumericType>
386 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMicrometre>::FromStandard(
387  NumericType& value) noexcept {
388  value *= static_cast<NumericType>(1000000000000.0L);
389 }
390 
391 template <>
392 template <typename NumericType>
393 inline constexpr void Conversion<Unit::Area, Unit::Area::SquareMicrometre>::ToStandard(
394  NumericType& value) noexcept {
395  value *= static_cast<NumericType>(0.000000000001L);
396 }
397 
398 template <>
399 template <typename NumericType>
400 inline 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 
405 template <>
406 template <typename NumericType>
407 inline 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 
412 template <typename NumericType>
413 inline 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 
446 template <typename NumericType>
447 inline 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> },
458  Conversions<Unit::Area, Unit::Area::Hectare>::ToStandard<NumericType> },
459  {Unit::Area::Acre, Conversions<Unit::Area, Unit::Area::Acre>::ToStandard<NumericType> },
461  Conversions<Unit::Area, Unit::Area::SquareYard>::ToStandard<NumericType> },
463  Conversions<Unit::Area, Unit::Area::SquareFoot>::ToStandard<NumericType> },
465  Conversions<Unit::Area, Unit::Area::SquareDecimetre>::ToStandard<NumericType> },
467  Conversions<Unit::Area, Unit::Area::SquareInch>::ToStandard<NumericType> },
469  Conversions<Unit::Area, Unit::Area::SquareCentimetre>::ToStandard<NumericType> },
471  Conversions<Unit::Area, Unit::Area::SquareMillimetre>::ToStandard<NumericType> },
473  Conversions<Unit::Area, Unit::Area::SquareMilliinch>::ToStandard<NumericType> },
475  Conversions<Unit::Area, Unit::Area::SquareMicrometre>::ToStandard<NumericType> },
477  Conversions<Unit::Area, Unit::Area::SquareMicroinch>::ToStandard<NumericType> },
478 };
479 
480 } // namespace Internal
481 
482 } // namespace PhQ
483 
484 #endif // PHQ_UNIT_AREA_HPP
ElectricCurrent
Electric current units.
Mass
Mass units.
Definition: Mass.hpp:53
Length
Length units.
Definition: Length.hpp:53
SubstanceAmount
Amount of substance units.
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.
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)