Debian Codegraph @

src/Base/Unit.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /****************************************************************************
3 * *
4 * This file is part of FreeCAD. *
5 * *
6 * FreeCAD is free software: you can redistribute it and/or modify it *
7 * under the terms of the GNU Lesser General Public License as *
8 * published by the Free Software Foundation, either version 2.1 of the *
9 * License, or (at your option) any later version. *
10 * *
11 * FreeCAD is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with FreeCAD. If not, see *
18 * <https://www.gnu.org/licenses/>. *
19 * *
20 ***************************************************************************/
21
22 #ifndef BASE_Unit_H
23 #define BASE_Unit_H
24
25 #include <array>
26 #include <cstdint>
27 #include <stdexcept>
28 #include <string>
29 #include <string_view>
30 #include <vector>
31
32 #include <FCGlobal.h>
33
34 #include "Exception.h"
35
36 namespace Base
37 {
38
39 constexpr auto unitSymbols = std::to_array<std::string_view>(
40 {"mm", "kg", "s", "A", "K", "mol", "cd", "deg"}
41 );
42
43 constexpr auto unitNumExponents {unitSymbols.size()};
44 using UnitExponents = std::array<int8_t, unitNumExponents>;
45
46 constexpr auto unitExponentLimit {8};
47
48 class BaseExport Unit final
49 {
50 public:
51 Unit() = default;
52
53 explicit constexpr Unit(const UnitExponents exps, const std::string_view name = "")
54 : _exps {exps}
55 , _name {name}
56 {
57 checkRange();
58 }
59
60 /// helper constructor to ease Unit construction from Python
61 explicit Unit(
62 const int length,
63 const int mass = 0,
64 const int time = 0,
65 const int electricCurrent = 0,
66 const int thermodynamicTemperature = 0,
67 const int amountOfSubstance = 0,
68 const int luminousIntensity = 0,
69 const int angle = 0
70 );
71
72 bool operator==(const Unit&) const;
73 bool operator!=(const Unit& that) const;
74 Unit& operator*=(const Unit& that);
75 Unit& operator/=(const Unit& that);
76 Unit operator*(const Unit&) const;
77 Unit operator/(const Unit&) const;
78
79 [[nodiscard]] Unit pow(const double exp) const;
80 [[nodiscard]] Unit root(const uint8_t num) const;
81
82 [[nodiscard]] UnitExponents exponents() const;
83 [[nodiscard]] int length() const;
84
85 [[nodiscard]] std::string getString() const; // E.g. kg, mm^2, mm*kg/s^2
86 [[nodiscard]] std::string getTypeString() const; // E.g. "Area", "Length", "Pressure"
87 [[nodiscard]] std::string representation() const; // E.g. "Unit: mm (1,0,0,0,0,0,0,0) [Length]"
88
89 Unit sqrt() const
90 {
91 return root(2);
92 }
93 Unit cbrt() const
94 {
95 return root(3);
96 }
97
98 private:
99 UnitExponents _exps {};
100 std::string_view _name;
101
102 constexpr void checkRange()
103 {
104 for (const auto exp : _exps) {
105 if (exp >= unitExponentLimit) {
106 throw OverflowError("Unit exponent overflow");
107 }
108 if (exp < -unitExponentLimit) {
109 throw UnderflowError("Unit exponent underflow");
110 }
111 }
112 }
113
114 /** Returns posIndexes, negIndexes*/
115 std::pair<std::vector<size_t>, std::vector<size_t>> nonZeroValsIndexes() const;
116
117 public:
118 static const Unit Acceleration;
119 static const Unit AmountOfSubstance;
120 static const Unit Angle;
121 static const Unit AngleOfFriction;
122 static const Unit Area;
123 static const Unit CompressiveStrength;
124 static const Unit CurrentDensity;
125 static const Unit Density;
126 static const Unit DissipationRate;
127 static const Unit DynamicViscosity;
128 static const Unit ElectricalCapacitance;
129 static const Unit ElectricalConductance;
130 static const Unit ElectricalConductivity;
131 static const Unit ElectricalInductance;
132 static const Unit ElectricalResistance;
133 static const Unit ElectricCharge;
134 static const Unit ElectricCurrent;
135 static const Unit ElectricPotential;
136 static const Unit ElectromagneticPotential;
137 static const Unit Force;
138 static const Unit Frequency;
139 static const Unit HeatFlux;
140 static const Unit InverseArea;
141 static const Unit InverseLength;
142 static const Unit InverseVolume;
143 static const Unit KinematicViscosity;
144 static const Unit Length;
145 static const Unit LuminousIntensity;
146 static const Unit MagneticFieldStrength;
147 static const Unit MagneticFlux;
148 static const Unit MagneticFluxDensity;
149 static const Unit Magnetization;
150 static const Unit Mass;
151 static const Unit Moment;
152 static const Unit One;
153 static const Unit Pressure;
154 static const Unit Power;
155 static const Unit ShearModulus;
156 static const Unit SpecificEnergy;
157 static const Unit SpecificHeat;
158 static const Unit Stiffness;
159 static const Unit StiffnessDensity;
160 static const Unit Stress;
161 static const Unit SurfaceChargeDensity;
162 static const Unit Temperature;
163 static const Unit TimeSpan;
164 static const Unit ThermalConductivity;
165 static const Unit ThermalExpansionCoefficient;
166 static const Unit ThermalTransferCoefficient;
167 static const Unit UltimateTensileStrength;
168 static const Unit VacuumPermittivity;
169 static const Unit Velocity;
170 static const Unit Volume;
171 static const Unit VolumeChargeDensity;
172 static const Unit VolumeFlowRate;
173 static const Unit VolumetricThermalExpansionCoefficient;
174 static const Unit Work;
175 static const Unit YieldStrength;
176 static const Unit YoungsModulus;
177 };
178
179 } // namespace Base
180
181 #endif // BASE_Unit_H