Units: Add Density to internal schema
Signed-off-by: Przemo Firszt <przemo@firszt.eu>
This commit is contained in:
parent
d380187ef3
commit
7d8fb99cea
|
@ -32,7 +32,8 @@
|
|||
using namespace Base;
|
||||
|
||||
static inline void checkRange(const char * op, int length, int mass, int time, int electricCurrent,
|
||||
int thermodynamicTemperature, int amountOfSubstance, int luminoseIntensity, int angle)
|
||||
int thermodynamicTemperature, int amountOfSubstance, int luminoseIntensity, int angle,
|
||||
int density)
|
||||
{
|
||||
if ( ( length >= (1 << (UnitSignatureLengthBits - 1)) ) ||
|
||||
( mass >= (1 << (UnitSignatureMassBits - 1)) ) ||
|
||||
|
@ -41,7 +42,8 @@ static inline void checkRange(const char * op, int length, int mass, int time, i
|
|||
( thermodynamicTemperature >= (1 << (UnitSignatureThermodynamicTemperatureBits - 1)) ) ||
|
||||
( amountOfSubstance >= (1 << (UnitSignatureAmountOfSubstanceBits - 1)) ) ||
|
||||
( luminoseIntensity >= (1 << (UnitSignatureLuminoseIntensityBits - 1)) ) ||
|
||||
( angle >= (1 << (UnitSignatureAngleBits - 1)) ) )
|
||||
( angle >= (1 << (UnitSignatureAngleBits - 1)) ) ||
|
||||
( density >= (1 << (UnitSignatureDensityBits - 1)) ) )
|
||||
throw Base::Exception((std::string("Unit overflow in ") + std::string(op)).c_str());
|
||||
if ( ( length < -(1 << (UnitSignatureLengthBits - 1)) ) ||
|
||||
( mass < -(1 << (UnitSignatureMassBits - 1)) ) ||
|
||||
|
@ -50,7 +52,8 @@ static inline void checkRange(const char * op, int length, int mass, int time, i
|
|||
( thermodynamicTemperature < -(1 << (UnitSignatureThermodynamicTemperatureBits - 1)) ) ||
|
||||
( amountOfSubstance < -(1 << (UnitSignatureAmountOfSubstanceBits - 1)) ) ||
|
||||
( luminoseIntensity < -(1 << (UnitSignatureLuminoseIntensityBits - 1)) ) ||
|
||||
( angle < -(1 << (UnitSignatureAngleBits - 1)) ) )
|
||||
( angle < -(1 << (UnitSignatureAngleBits - 1)) ) ||
|
||||
( density < -(1 << (UnitSignatureDensityBits - 1)) ) )
|
||||
throw Base::Exception((std::string("Unit underflow in ") + std::string(op)).c_str());
|
||||
}
|
||||
|
||||
|
@ -61,7 +64,8 @@ Unit::Unit(int8_t Length,
|
|||
int8_t ThermodynamicTemperature,
|
||||
int8_t AmountOfSubstance,
|
||||
int8_t LuminoseIntensity,
|
||||
int8_t Angle)
|
||||
int8_t Angle,
|
||||
int8_t Density)
|
||||
{
|
||||
checkRange("unit",
|
||||
(int32_t)Length,
|
||||
|
@ -71,7 +75,8 @@ Unit::Unit(int8_t Length,
|
|||
(int32_t)ThermodynamicTemperature,
|
||||
(int32_t)AmountOfSubstance,
|
||||
(int32_t)LuminoseIntensity,
|
||||
(int32_t)Angle);
|
||||
(int32_t)Angle,
|
||||
(int32_t)Density);
|
||||
|
||||
Sig.Length = Length;
|
||||
Sig.Mass = Mass;
|
||||
|
@ -81,6 +86,7 @@ Unit::Unit(int8_t Length,
|
|||
Sig.AmountOfSubstance = AmountOfSubstance;
|
||||
Sig.LuminoseIntensity = LuminoseIntensity;
|
||||
Sig.Angle = Angle;
|
||||
Sig.Density = Density;
|
||||
}
|
||||
|
||||
|
||||
|
@ -94,6 +100,7 @@ Unit::Unit()
|
|||
Sig.AmountOfSubstance = 0;
|
||||
Sig.LuminoseIntensity = 0;
|
||||
Sig.Angle = 0;
|
||||
Sig.Density = 0;
|
||||
}
|
||||
|
||||
Unit::Unit(const Unit& that)
|
||||
|
@ -116,7 +123,8 @@ Unit Unit::pow(char exp)const
|
|||
(int32_t)Sig.ThermodynamicTemperature * (int32_t)exp,
|
||||
(int32_t)Sig.AmountOfSubstance * (int32_t)exp,
|
||||
(int32_t)Sig.LuminoseIntensity * (int32_t)exp,
|
||||
(int32_t)Sig.Angle * (int32_t)exp);
|
||||
(int32_t)Sig.Angle * (int32_t)exp,
|
||||
(int32_t)Sig.Density * (int32_t)exp);
|
||||
|
||||
Unit result;
|
||||
result.Sig.Length = Sig.Length * exp;
|
||||
|
@ -127,6 +135,7 @@ Unit Unit::pow(char exp)const
|
|||
result.Sig.AmountOfSubstance = Sig.AmountOfSubstance * exp;
|
||||
result.Sig.LuminoseIntensity = Sig.LuminoseIntensity * exp;
|
||||
result.Sig.Angle = Sig.Angle * exp;
|
||||
result.Sig.Density = Sig.Density * exp;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -140,7 +149,8 @@ bool Unit::isEmpty(void)const
|
|||
&& (this->Sig.ThermodynamicTemperature == 0)
|
||||
&& (this->Sig.AmountOfSubstance == 0)
|
||||
&& (this->Sig.LuminoseIntensity == 0)
|
||||
&& (this->Sig.Angle == 0);
|
||||
&& (this->Sig.Angle == 0)
|
||||
&& (this->Sig.Density == 0);
|
||||
}
|
||||
|
||||
bool Unit::operator ==(const Unit& that) const
|
||||
|
@ -152,7 +162,8 @@ bool Unit::operator ==(const Unit& that) const
|
|||
&& (this->Sig.ThermodynamicTemperature == that.Sig.ThermodynamicTemperature)
|
||||
&& (this->Sig.AmountOfSubstance == that.Sig.AmountOfSubstance)
|
||||
&& (this->Sig.LuminoseIntensity == that.Sig.LuminoseIntensity)
|
||||
&& (this->Sig.Angle == that.Sig.Angle);
|
||||
&& (this->Sig.Angle == that.Sig.Angle)
|
||||
&& (this->Sig.Density == that.Sig.Density);
|
||||
}
|
||||
|
||||
|
||||
|
@ -166,7 +177,8 @@ Unit Unit::operator *(const Unit &right) const
|
|||
(int32_t)Sig.ThermodynamicTemperature + (int32_t)right.Sig.ThermodynamicTemperature,
|
||||
(int32_t)Sig.AmountOfSubstance + (int32_t)right.Sig.AmountOfSubstance,
|
||||
(int32_t)Sig.LuminoseIntensity + (int32_t)right.Sig.LuminoseIntensity,
|
||||
(int32_t)Sig.Angle + (int32_t)right.Sig.Angle);
|
||||
(int32_t)Sig.Angle + (int32_t)right.Sig.Angle,
|
||||
(int32_t)Sig.Density + (int32_t)right.Sig.Density);
|
||||
|
||||
Unit result;
|
||||
result.Sig.Length = Sig.Length + right.Sig.Length;
|
||||
|
@ -177,6 +189,7 @@ Unit Unit::operator *(const Unit &right) const
|
|||
result.Sig.AmountOfSubstance = Sig.AmountOfSubstance + right.Sig.AmountOfSubstance;
|
||||
result.Sig.LuminoseIntensity = Sig.LuminoseIntensity + right.Sig.LuminoseIntensity;
|
||||
result.Sig.Angle = Sig.Angle + right.Sig.Angle;
|
||||
result.Sig.Density = Sig.Density + right.Sig.Density;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -191,7 +204,8 @@ Unit Unit::operator /(const Unit &right) const
|
|||
(int32_t)Sig.ThermodynamicTemperature - (int32_t)right.Sig.ThermodynamicTemperature,
|
||||
(int32_t)Sig.AmountOfSubstance - (int32_t)right.Sig.AmountOfSubstance,
|
||||
(int32_t)Sig.LuminoseIntensity - (int32_t)right.Sig.LuminoseIntensity,
|
||||
(int32_t)Sig.Angle - (int32_t)right.Sig.Angle);
|
||||
(int32_t)Sig.Angle - (int32_t)right.Sig.Angle,
|
||||
(int32_t)Sig.Density - (int32_t)right.Sig.Density);
|
||||
|
||||
Unit result;
|
||||
result.Sig.Length = Sig.Length - right.Sig.Length;
|
||||
|
@ -202,6 +216,7 @@ Unit Unit::operator /(const Unit &right) const
|
|||
result.Sig.AmountOfSubstance = Sig.AmountOfSubstance - right.Sig.AmountOfSubstance;
|
||||
result.Sig.LuminoseIntensity = Sig.LuminoseIntensity - right.Sig.LuminoseIntensity;
|
||||
result.Sig.Angle = Sig.Angle - right.Sig.Angle;
|
||||
result.Sig.Density = Sig.Density - right.Sig.Density;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -216,6 +231,7 @@ Unit& Unit::operator = (const Unit &New)
|
|||
Sig.AmountOfSubstance = New.Sig.AmountOfSubstance ;
|
||||
Sig.LuminoseIntensity = New.Sig.LuminoseIntensity ;
|
||||
Sig.Angle = New.Sig.Angle ;
|
||||
Sig.Density = New.Sig.Density ;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
@ -234,7 +250,8 @@ QString Unit::getString(void) const
|
|||
Sig.ThermodynamicTemperature> 0 ||
|
||||
Sig.AmountOfSubstance > 0 ||
|
||||
Sig.LuminoseIntensity > 0 ||
|
||||
Sig.Angle > 0 ){
|
||||
Sig.Angle > 0 ||
|
||||
Sig.Density > 0 ){
|
||||
|
||||
bool mult = false;
|
||||
if(Sig.Length > 0){
|
||||
|
@ -292,6 +309,13 @@ QString Unit::getString(void) const
|
|||
if(Sig.Angle >1)
|
||||
ret << "^" << Sig.Angle;
|
||||
}
|
||||
if(Sig.Density > 0){
|
||||
if(mult) ret<<'*';
|
||||
mult = true;
|
||||
ret << "kg/m^3";
|
||||
if(Sig.Density >1)
|
||||
ret << "^" << Sig.Density;
|
||||
}
|
||||
}else{
|
||||
ret << "1";
|
||||
}
|
||||
|
@ -303,7 +327,8 @@ QString Unit::getString(void) const
|
|||
Sig.ThermodynamicTemperature< 0 ||
|
||||
Sig.AmountOfSubstance < 0 ||
|
||||
Sig.LuminoseIntensity < 0 ||
|
||||
Sig.Angle < 0 ){
|
||||
Sig.Angle < 0 ||
|
||||
Sig.Density < 0 ){
|
||||
ret << "/";
|
||||
|
||||
int nnom = Sig.Length<0?1:2 +
|
||||
|
@ -314,6 +339,7 @@ QString Unit::getString(void) const
|
|||
Sig.AmountOfSubstance<0?1:2 +
|
||||
Sig.LuminoseIntensity<0?1:2 +
|
||||
Sig.Angle<0?1:2 ;
|
||||
Sig.Density<0?1:2 ;
|
||||
if (nnom > 1) ret << '(';
|
||||
bool mult=false;
|
||||
if(Sig.Length < 0){
|
||||
|
@ -371,6 +397,13 @@ QString Unit::getString(void) const
|
|||
if(Sig.Angle <-1)
|
||||
ret << "^" << abs(Sig.Angle);
|
||||
}
|
||||
if(Sig.Density < 0){
|
||||
if(mult) ret<<'*';
|
||||
mult = true;
|
||||
ret << "kg/m^3";
|
||||
if(Sig.Density <-1)
|
||||
ret << "^" << abs(Sig.Density);
|
||||
}
|
||||
if (nnom > 1) ret << ')';
|
||||
}
|
||||
|
||||
|
@ -384,6 +417,7 @@ QString Unit::getTypeString(void) const
|
|||
if(*this == Unit::Volume ) return QString::fromLatin1("Volume"); else
|
||||
if(*this == Unit::Mass ) return QString::fromLatin1("Mass"); else
|
||||
if(*this == Unit::Angle ) return QString::fromLatin1("Angle"); else
|
||||
if(*this == Unit::Density ) return QString::fromLatin1("Density"); else
|
||||
if(*this == Unit::TimeSpan ) return QString::fromLatin1("TimeSpan"); else
|
||||
if(*this == Unit::Velocity ) return QString::fromLatin1("Velocity"); else
|
||||
if(*this == Unit::Acceleration ) return QString::fromLatin1("Acceleration"); else
|
||||
|
@ -404,6 +438,7 @@ Unit Unit::Area(2);
|
|||
Unit Unit::Volume(3);
|
||||
Unit Unit::Mass(0,1);
|
||||
Unit Unit::Angle(0,0,0,0,0,0,0,1);
|
||||
Unit Unit::Density(-3,1);
|
||||
|
||||
Unit Unit::TimeSpan(0,0,1);
|
||||
Unit Unit::Velocity(1,0,-1);
|
||||
|
|
|
@ -42,6 +42,7 @@ namespace Base {
|
|||
#define UnitSignatureAmountOfSubstanceBits 4
|
||||
#define UnitSignatureLuminoseIntensityBits 4
|
||||
#define UnitSignatureAngleBits 4
|
||||
#define UnitSignatureDensityBits 4
|
||||
|
||||
struct UnitSignature{
|
||||
int32_t Length:UnitSignatureLengthBits;
|
||||
|
@ -52,6 +53,7 @@ struct UnitSignature{
|
|||
int32_t AmountOfSubstance:UnitSignatureAmountOfSubstanceBits;
|
||||
int32_t LuminoseIntensity:UnitSignatureLuminoseIntensityBits;
|
||||
int32_t Angle:UnitSignatureAngleBits;
|
||||
int32_t Density:UnitSignatureDensityBits;
|
||||
};
|
||||
/**
|
||||
* The Unit class.
|
||||
|
@ -60,7 +62,7 @@ class BaseExport Unit
|
|||
{
|
||||
public:
|
||||
/// default constructor
|
||||
Unit(int8_t Length,int8_t Mass=0,int8_t Time=0,int8_t ElectricCurrent=0,int8_t ThermodynamicTemperature=0,int8_t AmountOfSubstance=0,int8_t LuminoseIntensity=0,int8_t Angle=0);
|
||||
Unit(int8_t Length,int8_t Mass=0,int8_t Time=0,int8_t ElectricCurrent=0,int8_t ThermodynamicTemperature=0,int8_t AmountOfSubstance=0,int8_t LuminoseIntensity=0,int8_t Angle=0, int8_t Density=0);
|
||||
Unit(void);
|
||||
Unit(const Unit&);
|
||||
Unit(const std::string& Pars);
|
||||
|
@ -95,6 +97,7 @@ public:
|
|||
static Unit Mass;
|
||||
/// Angle
|
||||
static Unit Angle;
|
||||
static Unit Density;
|
||||
|
||||
static Unit Area;
|
||||
static Unit Volume;
|
||||
|
|
|
@ -80,6 +80,17 @@ QString UnitsSchemaInternal::schemaTranslate(Base::Quantity quant,double &factor
|
|||
// default action for all cases without special treatment:
|
||||
unitString = quant.getUnit().getString();
|
||||
factor = 1.0;
|
||||
}else if (unit == Unit::Density){
|
||||
if(UnitValue < 0.0001){
|
||||
unitString = QString::fromLatin1("kg/m^3");
|
||||
factor = 0.000000001;
|
||||
}else if(UnitValue < 1.0){
|
||||
unitString = QString::fromLatin1("kg/cm^3");
|
||||
factor = 0.001;
|
||||
}else{
|
||||
unitString = QString::fromLatin1("kg/mm^3");
|
||||
factor = 1.0;
|
||||
}
|
||||
}else if ((unit == Unit::Pressure) || (unit == Unit::Stress)){
|
||||
if(UnitValue < 10.0){// Pa is the smallest
|
||||
unitString = QString::fromLatin1("Pa");
|
||||
|
|
Loading…
Reference in New Issue
Block a user