Base: UnitsSchema: return std::string

This commit is contained in:
Ladislav Michl
2024-07-13 17:19:27 +02:00
committed by Yorik van Havre
parent 2ea8a633ac
commit 4d6ed9d531
25 changed files with 493 additions and 589 deletions

View File

@@ -26,17 +26,13 @@
#include <unistd.h>
#endif
#include <QString>
#include "UnitsSchemaInternal.h"
#include <cmath>
using namespace Base;
QString
UnitsSchemaInternal::schemaTranslate(const Quantity& quant, double& factor, QString& unitString)
std::string
UnitsSchemaInternal::schemaTranslate(const Quantity& quant, double& factor, std::string& unitString)
{
double UnitValue = std::abs(quant.getValue());
Unit unit = quant.getUnit();
@@ -52,585 +48,585 @@ UnitsSchemaInternal::schemaTranslate(const Quantity& quant, double& factor, QStr
// now do special treatment on all cases seems necessary:
if (unit == Unit::Length) { // Length handling ============================
if (UnitValue < 1e-6) { // smaller than 0.001 nm -> scientific notation
unitString = QString::fromLatin1("mm");
unitString = "mm";
factor = 1.0;
}
else if (UnitValue < 1e-3) {
unitString = QString::fromLatin1("nm");
unitString = "nm";
factor = 1e-6;
}
else if (UnitValue < 0.1) {
unitString = QString::fromUtf8("\xC2\xB5m");
unitString = "\xC2\xB5m";
factor = 1e-3;
}
else if (UnitValue < 1e4) {
unitString = QString::fromLatin1("mm");
unitString = "mm";
factor = 1.0;
}
else if (UnitValue < 1e7) {
unitString = QString::fromLatin1("m");
unitString = "m";
factor = 1e3;
}
else if (UnitValue < 1e10) {
unitString = QString::fromLatin1("km");
unitString = "km";
factor = 1e6;
}
else { // bigger than 1000 km -> scientific notation
unitString = QString::fromLatin1("m");
unitString = "m";
factor = 1e3;
}
}
else if (unit == Unit::Area) {
if (UnitValue < 100) {
unitString = QString::fromLatin1("mm^2");
unitString = "mm^2";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("cm^2");
unitString = "cm^2";
factor = 100;
}
else if (UnitValue < 1e12) {
unitString = QString::fromLatin1("m^2");
unitString = "m^2";
factor = 1e6;
}
else { // bigger than 1 square kilometer
unitString = QString::fromLatin1("km^2");
unitString = "km^2";
factor = 1e12;
}
}
else if (unit == Unit::Volume) {
if (UnitValue < 1e3) { // smaller than 1 ul
unitString = QString::fromLatin1("mm^3");
unitString = "mm^3";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("ml");
unitString = "ml";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("l");
unitString = "l";
factor = 1e6;
}
else { // bigger than 1000 l
unitString = QString::fromLatin1("m^3");
unitString = "m^3";
factor = 1e9;
}
}
else if (unit == Unit::Angle) {
// TODO: Cascade for the Areas
// default action for all cases without special treatment:
unitString = QString::fromUtf8("\xC2\xB0");
unitString = "\xC2\xB0";
factor = 1.0;
}
else if (unit == Unit::Mass) {
if (UnitValue < 1e-6) {
unitString = QString::fromUtf8("\xC2\xB5g");
unitString = "\xC2\xB5g";
factor = 1e-9;
}
else if (UnitValue < 1e-3) {
unitString = QString::fromLatin1("mg");
unitString = "mg";
factor = 1e-6;
}
else if (UnitValue < 1.0) {
unitString = QString::fromLatin1("g");
unitString = "g";
factor = 1e-3;
}
else if (UnitValue < 1e3) {
unitString = QString::fromLatin1("kg");
unitString = "kg";
factor = 1.0;
}
else {
unitString = QString::fromLatin1("t");
unitString = "t";
factor = 1e3;
}
}
else if (unit == Unit::Density) {
if (UnitValue < 0.0001) {
unitString = QString::fromLatin1("kg/m^3");
unitString = "kg/m^3";
factor = 1e-9;
}
else if (UnitValue < 1.0) {
unitString = QString::fromLatin1("kg/cm^3");
unitString = "kg/cm^3";
factor = 0.001;
}
else {
unitString = QString::fromLatin1("kg/mm^3");
unitString = "kg/mm^3";
factor = 1.0;
}
}
else if (unit == Unit::ThermalConductivity) {
if (UnitValue > 1e6) {
unitString = QString::fromLatin1("W/mm/K");
unitString = "W/mm/K";
factor = 1e6;
}
else {
unitString = QString::fromLatin1("W/m/K");
unitString = "W/m/K";
factor = 1000.0;
}
}
else if (unit == Unit::ThermalExpansionCoefficient) {
if (UnitValue < 0.001) {
unitString = QString::fromUtf8("\xC2\xB5m/m/K"); // micro-meter/meter/K
unitString = "\xC2\xB5m/m/K"; // micro-meter/meter/K
factor = 1e-6;
}
else {
unitString = QString::fromLatin1("mm/mm/K");
unitString = "mm/mm/K";
factor = 1.0;
}
}
else if (unit == Unit::VolumetricThermalExpansionCoefficient) {
if (UnitValue < 0.001) {
unitString = QString::fromUtf8("mm^3/m^3/K");
unitString = "mm^3/m^3/K";
factor = 1e-9;
}
else {
unitString = QString::fromLatin1("m^3/m^3/K");
unitString = "m^3/m^3/K";
factor = 1.0;
}
}
else if (unit == Unit::SpecificHeat) {
unitString = QString::fromLatin1("J/kg/K");
unitString = "J/kg/K";
factor = 1e6;
}
else if (unit == Unit::ThermalTransferCoefficient) {
unitString = QString::fromLatin1("W/m^2/K");
unitString = "W/m^2/K";
factor = 1.0;
}
else if ((unit == Unit::Pressure) || (unit == Unit::Stress)) {
if (UnitValue < 10.0) { // Pa is the smallest
unitString = QString::fromLatin1("Pa");
unitString = "Pa";
factor = 0.001;
}
else if (UnitValue < 10000.0) {
unitString = QString::fromLatin1("kPa");
unitString = "kPa";
factor = 1.0;
}
else if (UnitValue < 10000000.0) {
unitString = QString::fromLatin1("MPa");
unitString = "MPa";
factor = 1000.0;
}
else if (UnitValue < 10000000000.0) {
unitString = QString::fromLatin1("GPa");
unitString = "GPa";
factor = 1e6;
}
else { // bigger -> scientific notation
unitString = QString::fromLatin1("Pa");
unitString = "Pa";
factor = 0.001;
}
}
else if ((unit == Unit::Stiffness)) {
if (UnitValue < 1) { // mN/m is the smallest
unitString = QString::fromLatin1("mN/m");
unitString = "mN/m";
factor = 1e-3;
}
else if (UnitValue < 1e3) {
unitString = QString::fromLatin1("N/m");
unitString = "N/m";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("kN/m");
unitString = "kN/m";
factor = 1e3;
}
else {
unitString = QString::fromLatin1("MN/m");
unitString = "MN/m";
factor = 1e6;
}
}
else if ((unit == Unit::StiffnessDensity)) {
if (UnitValue < 1e-3) {
unitString = QString::fromLatin1("Pa/m");
unitString = "Pa/m";
factor = 1e-6;
}
else if (UnitValue < 1) {
unitString = QString::fromLatin1("kPa/m");
unitString = "kPa/m";
factor = 1e-3;
}
else if (UnitValue < 1e3) {
unitString = QString::fromLatin1("MPa/m");
unitString = "MPa/m";
factor = 1.0;
}
else {
unitString = QString::fromLatin1("GPa/m");
unitString = "GPa/m";
factor = 1e3;
}
}
else if (unit == Unit::Force) {
if (UnitValue < 1e3) {
unitString = QString::fromLatin1("mN");
unitString = "mN";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("N");
unitString = "N";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("kN");
unitString = "kN";
factor = 1e6;
}
else {
unitString = QString::fromLatin1("MN");
unitString = "MN";
factor = 1e9;
}
}
// else if (unit == Unit::Moment) {
// if (UnitValue < 1e6) {
// unitString = QString::fromLatin1("mNm");
// unitString = "mNm";
// factor = 1e3;
// }
// else if (UnitValue < 1e9) {
// unitString = QString::fromLatin1("Nm");
// unitString = "Nm";
// factor = 1e6;
// }
// else if (UnitValue < 1e12) {
// unitString = QString::fromLatin1("kNm");
// unitString = "kNm";
// factor = 1e9;
// }
// else {
// unitString = QString::fromLatin1("MNm");
// unitString = "MNm";
// factor = 1e12;
// }
// }
else if (unit == Unit::Power) {
if (UnitValue < 1e6) {
unitString = QString::fromLatin1("mW");
unitString = "mW";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("W");
unitString = "W";
factor = 1e6;
}
else {
unitString = QString::fromLatin1("kW");
unitString = "kW";
factor = 1e9;
}
}
else if (unit == Unit::ElectricPotential) {
if (UnitValue < 1e6) {
unitString = QString::fromLatin1("mV");
unitString = "mV";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("V");
unitString = "V";
factor = 1e6;
}
else if (UnitValue < 1e12) {
unitString = QString::fromLatin1("kV");
unitString = "kV";
factor = 1e9;
}
else { // > 1000 kV scientificc notation
unitString = QString::fromLatin1("V");
unitString = "V";
factor = 1e6;
}
}
else if (unit == Unit::Work) {
if (UnitValue < 1.602176634e-10) {
unitString = QString::fromLatin1("eV");
unitString = "eV";
factor = 1.602176634e-13;
}
else if (UnitValue < 1.602176634e-7) {
unitString = QString::fromLatin1("keV");
unitString = "keV";
factor = 1.602176634e-10;
}
else if (UnitValue < 1.602176634e-4) {
unitString = QString::fromLatin1("MeV");
unitString = "MeV";
factor = 1.602176634e-7;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("mJ");
unitString = "mJ";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("J");
unitString = "J";
factor = 1e6;
}
else if (UnitValue < 1e12) {
unitString = QString::fromLatin1("kJ");
unitString = "kJ";
factor = 1e9;
}
else if (UnitValue < 3.6e+15) {
unitString = QString::fromLatin1("kWh");
unitString = "kWh";
factor = 3.6e+12;
}
else { // bigger than 1000 kWh -> scientific notation
unitString = QString::fromLatin1("J");
unitString = "J";
factor = 1e6;
}
}
else if (unit == Unit::SpecificEnergy) {
unitString = QString::fromLatin1("m^2/s^2");
unitString = "m^2/s^2";
factor = 1e6;
}
else if (unit == Unit::HeatFlux) {
unitString = QString::fromLatin1("W/m^2");
unitString = "W/m^2";
factor = 1; // unit signature (0,1,-3,0,0) is length independent
}
else if (unit == Unit::ElectricCharge) {
unitString = QString::fromLatin1("C");
unitString = "C";
factor = 1.0;
}
else if (unit == Unit::CurrentDensity) {
if (UnitValue <= 1e3) {
unitString = QString::fromLatin1("A/m^2");
unitString = "A/m^2";
factor = 1e-6;
}
else {
unitString = QString::fromLatin1("A/mm^2");
unitString = "A/mm^2";
factor = 1;
}
}
else if (unit == Unit::MagneticFluxDensity) {
if (UnitValue <= 1e-3) {
unitString = QString::fromLatin1("G");
unitString = "G";
factor = 1e-4;
}
else {
unitString = QString::fromLatin1("T");
unitString = "T";
factor = 1.0;
}
}
else if (unit == Unit::MagneticFieldStrength) {
unitString = QString::fromLatin1("A/m");
unitString = "A/m";
factor = 1e-3;
}
else if (unit == Unit::MagneticFlux) {
unitString = QString::fromLatin1("Wb");
unitString = "Wb";
factor = 1e6;
}
else if (unit == Unit::Magnetization) {
unitString = QString::fromLatin1("A/m");
unitString = "A/m";
factor = 1e-3;
}
else if (unit == Unit::ElectromagneticPotential) {
unitString = QString::fromLatin1("Wb/m");
unitString = "Wb/m";
factor = 1e3;
}
else if (unit == Unit::ElectricalConductance) {
if (UnitValue < 1e-9) {
unitString = QString::fromUtf8("\xC2\xB5S");
unitString = "\xC2\xB5S";
factor = 1e-12;
}
else if (UnitValue < 1e-6) {
unitString = QString::fromLatin1("mS");
unitString = "mS";
factor = 1e-9;
}
else {
unitString = QString::fromLatin1("S");
unitString = "S";
factor = 1e-6;
}
}
else if (unit == Unit::ElectricalResistance) {
if (UnitValue < 1e9) {
unitString = QString::fromLatin1("Ohm");
unitString = "Ohm";
factor = 1e6;
}
else if (UnitValue < 1e12) {
unitString = QString::fromLatin1("kOhm");
unitString = "kOhm";
factor = 1e9;
}
else {
unitString = QString::fromLatin1("MOhm");
unitString = "MOhm";
factor = 1e12;
}
}
else if (unit == Unit::ElectricalConductivity) {
if (UnitValue < 1e-3) {
unitString = QString::fromLatin1("mS/m");
unitString = "mS/m";
factor = 1e-12;
}
else if (UnitValue < 1.0) {
unitString = QString::fromLatin1("S/m");
unitString = "S/m";
factor = 1e-9;
}
else if (UnitValue < 1e3) {
unitString = QString::fromLatin1("kS/m");
unitString = "kS/m";
factor = 1e-6;
}
else {
unitString = QString::fromLatin1("MS/m");
unitString = "MS/m";
factor = 1e-3;
}
}
else if (unit == Unit::ElectricalCapacitance) {
if (UnitValue < 1e-15) {
unitString = QString::fromLatin1("pF");
unitString = "pF";
factor = 1e-18;
}
else if (UnitValue < 1e-12) {
unitString = QString::fromLatin1("nF");
unitString = "nF";
factor = 1e-15;
}
else if (UnitValue < 1e-9) {
// \x reads everything to the end, therefore split
unitString = QString::fromUtf8("\xC2\xB5"
"F");
unitString = "\xC2\xB5"
"F";
factor = 1e-12;
}
else if (UnitValue < 1e-6) {
unitString = QString::fromLatin1("mF");
unitString = "mF";
factor = 1e-9;
}
else {
unitString = QString::fromLatin1("F");
unitString = "F";
factor = 1e-6;
}
}
else if (unit == Unit::ElectricalInductance) {
if (UnitValue < 1.0) {
unitString = QString::fromLatin1("nH");
unitString = "nH";
factor = 1e-3;
}
else if (UnitValue < 1e3) {
unitString = QString::fromUtf8("\xC2\xB5H");
unitString = "\xC2\xB5H";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("mH");
unitString = "mH";
factor = 1e3;
}
else {
unitString = QString::fromLatin1("H");
unitString = "H";
factor = 1e6;
}
}
else if (unit == Unit::VacuumPermittivity) {
unitString = QString::fromLatin1("F/m");
unitString = "F/m";
factor = 1e-9;
}
else if (unit == Unit::Frequency) {
if (UnitValue < 1e3) {
unitString = QString::fromLatin1("Hz");
unitString = "Hz";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("kHz");
unitString = "kHz";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("MHz");
unitString = "MHz";
factor = 1e6;
}
else if (UnitValue < 1e12) {
unitString = QString::fromLatin1("GHz");
unitString = "GHz";
factor = 1e9;
}
else {
unitString = QString::fromLatin1("THz");
unitString = "THz";
factor = 1e12;
}
}
else if (unit == Unit::Velocity) {
unitString = QString::fromLatin1("mm/s");
unitString = "mm/s";
factor = 1.0;
}
else if (unit == Unit::DynamicViscosity) {
unitString = QString::fromLatin1("Pa*s");
unitString = "Pa*s";
factor = 0.001;
}
else if (unit == Unit::KinematicViscosity) {
if (UnitValue < 1e3) {
unitString = QString::fromLatin1("mm^2/s");
unitString = "mm^2/s";
factor = 1.0;
}
else {
unitString = QString::fromLatin1("m^2/s");
unitString = "m^2/s";
factor = 1e6;
}
}
else if (unit == Unit::VolumeFlowRate) {
if (UnitValue < 1e3) {
unitString = QString::fromLatin1("mm^3/s");
unitString = "mm^3/s";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromLatin1("ml/s");
unitString = "ml/s";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("l/s");
unitString = "l/s";
factor = 1e6;
}
else {
unitString = QString::fromLatin1("m^3/s");
unitString = "m^3/s";
factor = 1e9;
}
}
else if (unit == Unit::DissipationRate) {
unitString = QString::fromLatin1("W/kg");
unitString = "W/kg";
factor = 1e6;
}
else if (unit == Unit::InverseLength) {
if (UnitValue < 1e-6) { // smaller than 0.001 1/km -> scientific notation
unitString = QString::fromLatin1("1/m");
unitString = "1/m";
factor = 1e-3;
}
else if (UnitValue < 1e-3) {
unitString = QString::fromLatin1("1/km");
unitString = "1/km";
factor = 1e-6;
}
else if (UnitValue < 1.0) {
unitString = QString::fromLatin1("1/m");
unitString = "1/m";
factor = 1e-3;
}
else if (UnitValue < 1e3) {
unitString = QString::fromLatin1("1/mm");
unitString = "1/mm";
factor = 1.0;
}
else if (UnitValue < 1e6) {
unitString = QString::fromUtf8("1/\xC2\xB5m");
unitString = "1/\xC2\xB5m";
factor = 1e3;
}
else if (UnitValue < 1e9) {
unitString = QString::fromLatin1("1/nm");
unitString = "1/nm";
factor = 1e6;
}
else { // larger -> scientific notation
unitString = QString::fromLatin1("1/m");
unitString = "1/m";
factor = 1e-3;
}
}
else if (unit == Unit::InverseArea) {
if (UnitValue < 1e-12) { // smaller than 0.001 1/km^2 -> scientific notation
unitString = QString::fromLatin1("1/m^2");
unitString = "1/m^2";
factor = 1e-6;
}
else if (UnitValue < 1e-6) {
unitString = QString::fromLatin1("1/km^2");
unitString = "1/km^2";
factor = 1e-12;
}
else if (UnitValue < 1.0) {
unitString = QString::fromLatin1("1/m^2");
unitString = "1/m^2";
factor = 1e-6;
}
else if (UnitValue < 1e2) {
unitString = QString::fromLatin1("1/cm^2");
unitString = "1/cm^2";
factor = 1e-2;
}
else {
unitString = QString::fromLatin1("1/mm^2");
unitString = "1/mm^2";
factor = 1.0;
}
}
else if (unit == Unit::InverseVolume) {
if (UnitValue < 1e-6) {
unitString = QString::fromLatin1("1/m^3");
unitString = "1/m^3";
factor = 1e-9;
}
else if (UnitValue < 1e-3) {
unitString = QString::fromLatin1("1/l");
unitString = "1/l";
factor = 1e-6;
}
else if (UnitValue < 1.0) {
unitString = QString::fromLatin1("1/ml");
unitString = "1/ml";
factor = 1e-3;
}
else {
unitString = QString::fromLatin1("1/mm^3");
unitString = "1/mm^3";
factor = 1.0;
}
}
else {
// default action for all cases without special treatment:
unitString = QString::fromStdString(quant.getUnit().getString());
unitString = quant.getUnit().getString();
factor = 1.0;
}