Ref T429, functions for angle, heading and situation

* normalize angle -180/+180 when set on situation
* clamp vector function
This commit is contained in:
Klaus Basan
2018-11-09 02:26:07 +01:00
parent 985c06816b
commit 7807a05f2a
8 changed files with 89 additions and 33 deletions

View File

@@ -49,7 +49,7 @@ namespace BlackMisc
CAircraftSituation::CAircraftSituation(const CCoordinateGeodetic &position, const CHeading &heading, const CAngle &pitch, const CAngle &bank, const CSpeed &gs, const CElevationPlane &groundElevation) CAircraftSituation::CAircraftSituation(const CCoordinateGeodetic &position, const CHeading &heading, const CAngle &pitch, const CAngle &bank, const CSpeed &gs, const CElevationPlane &groundElevation)
: m_position(position), m_groundElevationPlane(groundElevation), : m_position(position), m_groundElevationPlane(groundElevation),
m_heading(heading), m_pitch(pitch), m_bank(bank), m_heading(heading.normalizedToPlusMinus180Degrees()), m_pitch(pitch.normalizedToPlusMinus180Degrees()), m_bank(bank.normalizedToPlusMinus180Degrees()),
m_groundSpeed(gs) m_groundSpeed(gs)
{ {
m_pressureAltitude = position.geodeticHeight().toPressureAltitude(CPressure(1013.25, CPressureUnit::mbar())); m_pressureAltitude = position.geodeticHeight().toPressureAltitude(CPressure(1013.25, CPressureUnit::mbar()));
@@ -58,7 +58,7 @@ namespace BlackMisc
CAircraftSituation::CAircraftSituation(const CCallsign &correspondingCallsign, const CCoordinateGeodetic &position, const CHeading &heading, const CAngle &pitch, const CAngle &bank, const CSpeed &gs, const CElevationPlane &groundElevation) CAircraftSituation::CAircraftSituation(const CCallsign &correspondingCallsign, const CCoordinateGeodetic &position, const CHeading &heading, const CAngle &pitch, const CAngle &bank, const CSpeed &gs, const CElevationPlane &groundElevation)
: m_correspondingCallsign(correspondingCallsign), : m_correspondingCallsign(correspondingCallsign),
m_position(position), m_groundElevationPlane(groundElevation), m_position(position), m_groundElevationPlane(groundElevation),
m_heading(heading), m_pitch(pitch), m_bank(bank), m_heading(heading.normalizedToPlusMinus180Degrees()), m_pitch(pitch.normalizedToPlusMinus180Degrees()), m_bank(bank.normalizedToPlusMinus180Degrees()),
m_groundSpeed(gs) m_groundSpeed(gs)
{ {
m_correspondingCallsign.setTypeHint(CCallsign::Aircraft); m_correspondingCallsign.setTypeHint(CCallsign::Aircraft);
@@ -889,6 +889,11 @@ namespace BlackMisc
return ag; return ag;
} }
void CAircraftSituation::setHeading(const CHeading &heading)
{
m_heading = heading.normalizedToPlusMinus180Degrees();
}
const CLengthUnit &CAircraftSituation::getAltitudeOrDefaultUnit() const const CLengthUnit &CAircraftSituation::getAltitudeOrDefaultUnit() const
{ {
if (this->getAltitude().isNull()) { return CAltitude::defaultUnit(); } if (this->getAltitude().isNull()) { return CAltitude::defaultUnit(); }
@@ -989,6 +994,16 @@ namespace BlackMisc
m_pressureAltitude = altitude; m_pressureAltitude = altitude;
} }
void CAircraftSituation::setPitch(const CAngle &pitch)
{
m_pitch = pitch.normalizedToPlusMinus180Degrees();
}
void CAircraftSituation::setBank(const CAngle &bank)
{
m_bank = bank.normalizedToPlusMinus180Degrees();
}
void CAircraftSituation::setZeroPBH() void CAircraftSituation::setZeroPBH()
{ {
static const CAngle za(0, CAngleUnit::deg()); static const CAngle za(0, CAngleUnit::deg());

View File

@@ -362,7 +362,7 @@ namespace BlackMisc
const CHeading &getHeading() const { return m_heading; } const CHeading &getHeading() const { return m_heading; }
//! Set heading //! Set heading
void setHeading(const CHeading &heading) { m_heading = heading; } void setHeading(const CHeading &heading);
//! Get altitude //! Get altitude
const CAltitude &getAltitude() const { return m_position.geodeticHeight(); } const CAltitude &getAltitude() const { return m_position.geodeticHeight(); }
@@ -405,13 +405,13 @@ namespace BlackMisc
const PhysicalQuantities::CAngle &getPitch() const { return m_pitch; } const PhysicalQuantities::CAngle &getPitch() const { return m_pitch; }
//! Set pitch //! Set pitch
void setPitch(const PhysicalQuantities::CAngle &pitch) { m_pitch = pitch; } void setPitch(const PhysicalQuantities::CAngle &pitch);
//! Get bank (angle) //! Get bank (angle)
const PhysicalQuantities::CAngle &getBank() const { return m_bank; } const PhysicalQuantities::CAngle &getBank() const { return m_bank; }
//! Set bank (angle) //! Set bank (angle)
void setBank(const PhysicalQuantities::CAngle &bank) { m_bank = bank; } void setBank(const PhysicalQuantities::CAngle &bank);
//! Set PBH values to 0 (zero) //! Set PBH values to 0 (zero)
void setZeroPBH(); void setZeroPBH();

View File

@@ -21,18 +21,28 @@ namespace BlackMisc
QString CHeading::convertToQString(bool i18n) const QString CHeading::convertToQString(bool i18n) const
{ {
static const QString s("%1 %2"); static const QString s("%1 %2");
if (i18n) return i18n ?
{ s.arg(CAngle::convertToQString(i18n),
return s.arg(CAngle::convertToQString(i18n), this->isMagneticHeading() ?
this->isMagneticHeading() ? QCoreApplication::translate("Aviation", "magnetic") :
QCoreApplication::translate("Aviation", "magnetic") : QCoreApplication::translate("Aviation", "true")) :
QCoreApplication::translate("Aviation", "true")); s.arg(CAngle::convertToQString(i18n),
} this->isMagneticHeading() ? "magnetic" : "true");
else }
{
return s.arg(CAngle::convertToQString(i18n), void CHeading::normalizeToPlusMinus180Degrees()
this->isMagneticHeading() ? "magnetic" : "true"); {
} const double v = normalizeDegrees180(this->value(CAngleUnit::deg()));
const CAngleUnit u = this->getUnit();
*this = CHeading(v, this->getReferenceNorth(), CAngleUnit::deg());
this->switchUnit(u);
}
CHeading CHeading::normalizedToPlusMinus180Degrees() const
{
CHeading copy(*this);
copy.normalizeToPlusMinus180Degrees();
return copy;
} }
void CHeading::registerMetadata() void CHeading::registerMetadata()

View File

@@ -87,6 +87,12 @@ namespace BlackMisc
//! Get reference north (magnetic or true) //! Get reference north (magnetic or true)
ReferenceNorth getReferenceNorth() const { return m_north; } ReferenceNorth getReferenceNorth() const { return m_north; }
//! Normalize to +- 180deg, [-179.99, 180.0]
void normalizeToPlusMinus180Degrees();
//! As [-179.99, 180.0] normalized heading
CHeading normalizedToPlusMinus180Degrees() const;
//! Register metadata //! Register metadata
static void registerMetadata(); static void registerMetadata();

View File

@@ -223,17 +223,23 @@ namespace BlackMisc
v[0] >= -l && v[1] >= -l && v[2] >= -l; v[0] >= -l && v[1] >= -l && v[2] >= -l;
} }
int CCoordinateGeodetic::clampVector()
{
int c = 0;
// *INDENT-OFF*
if (m_x < -1.0) { m_x = -1.0; c++; } else if (m_x > 1.0) { m_x = 1.0; c++; }
if (m_y < -1.0) { m_y = -1.0; c++; } else if (m_y > 1.0) { m_y = 1.0; c++; }
if (m_z < -1.0) { m_z = -1.0; c++; } else if (m_z > 1.0) { m_z = 1.0; c++; }
// *INDENT-ON*
return c;
}
CVariant CCoordinateGeodetic::propertyByIndex(const BlackMisc::CPropertyIndex &index) const CVariant CCoordinateGeodetic::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{ {
if (index.isMyself()) { return CVariant::from(*this); } if (index.isMyself()) { return CVariant::from(*this); }
if (ICoordinateGeodetic::canHandleIndex(index)) return (ICoordinateGeodetic::canHandleIndex(index)) ?
{ ICoordinateGeodetic::propertyByIndex(index) :
return ICoordinateGeodetic::propertyByIndex(index); CValueObject::propertyByIndex(index);
}
else
{
return CValueObject::propertyByIndex(index);
}
} }
void CCoordinateGeodetic::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant) void CCoordinateGeodetic::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
@@ -255,14 +261,9 @@ namespace BlackMisc
int CCoordinateGeodetic::comparePropertyByIndex(const CPropertyIndex &index, const CCoordinateGeodetic &compareValue) const int CCoordinateGeodetic::comparePropertyByIndex(const CPropertyIndex &index, const CCoordinateGeodetic &compareValue) const
{ {
if (ICoordinateGeodetic::canHandleIndex(index)) return ICoordinateGeodetic::canHandleIndex(index) ?
{ ICoordinateGeodetic::comparePropertyByIndex(index, compareValue) :
return ICoordinateGeodetic::comparePropertyByIndex(index, compareValue); CValueObject::comparePropertyByIndex(index, compareValue);
}
else
{
return CValueObject::comparePropertyByIndex(index, compareValue);
}
} }
CCoordinateGeodetic::CCoordinateGeodetic(const std::array<double, 3> &normalVector) CCoordinateGeodetic::CCoordinateGeodetic(const std::array<double, 3> &normalVector)

View File

@@ -299,6 +299,9 @@ namespace BlackMisc
//! Set normal vector //! Set normal vector
void setNormalVector(const std::array<double, 3> &normalVector); void setNormalVector(const std::array<double, 3> &normalVector);
//! Clamp vector values if out [-1, 1]
int clampVector();
//! Set to null //! Set to null
void setNull() void setNull()
{ {

View File

@@ -109,6 +109,21 @@ namespace BlackMisc
return std::tan(this->value(CAngleUnit::rad())); return std::tan(this->value(CAngleUnit::rad()));
} }
void CAngle::normalizeToPlusMinus180Degrees()
{
const double v = normalizeDegrees180(this->value(CAngleUnit::deg()));
const CAngleUnit u = this->getUnit();
*this = CAngle(v, CAngleUnit::deg());
this->switchUnit(u);
}
CAngle CAngle::normalizedToPlusMinus180Degrees() const
{
CAngle copy(*this);
copy.normalizeToPlusMinus180Degrees();
return copy;
}
double CAngle::normalizeDegrees180(double degrees, int roundDigits) double CAngle::normalizeDegrees180(double degrees, int roundDigits)
{ {
double d = CMathUtils::normalizeDegrees360(degrees + 180.0) - 180.0; double d = CMathUtils::normalizeDegrees360(degrees + 180.0) - 180.0;

View File

@@ -97,6 +97,12 @@ namespace BlackMisc
//! Tangent of angle //! Tangent of angle
double tan() const; double tan() const;
//! Normalize to +- 180deg, [-179.99, 180.0]
void normalizeToPlusMinus180Degrees();
//! As [-179.99, 180.0] normalized heading
CAngle normalizedToPlusMinus180Degrees() const;
//! Normalize: -180< degrees ≤180 //! Normalize: -180< degrees ≤180
static double normalizeDegrees180(double degrees, int roundDigits = -1); static double normalizeDegrees180(double degrees, int roundDigits = -1);