diff --git a/src/blackmisc/aviation/aircraftsituation.cpp b/src/blackmisc/aviation/aircraftsituation.cpp index 5da214392..19b4699cf 100644 --- a/src/blackmisc/aviation/aircraftsituation.cpp +++ b/src/blackmisc/aviation/aircraftsituation.cpp @@ -202,13 +202,12 @@ namespace BlackMisc return { 0, nullptr }; } - CAltitude CAircraftSituation::getCorrectedAltitude(const CLength &cgAboveGround) const + CAltitude CAircraftSituation::getCorrectedAltitude() const { - if (!this->hasGroundElevation()) { return this->getAltitude(); } - const CAltitude groundElevation(cgAboveGround.isNull() ? - this->getGroundElevation() : - CAltitude(this->getGroundElevation() + cgAboveGround, CAltitude::MeanSeaLevel)); - return (groundElevation <= this->getAltitude()) ? this->getAltitude() : groundElevation; + if (this->getGroundElevation().isNull()) { return this->getAltitude(); } + if (this->getAltitude().getReferenceDatum() != CAltitude::MeanSeaLevel) { return this->getAltitude(); } + if (this->getGroundElevation() < this->getAltitude()) { return this->getAltitude(); } + return this->getGroundElevation(); } void CAircraftSituation::setCallsign(const CCallsign &callsign) diff --git a/src/blackmisc/aviation/aircraftsituation.h b/src/blackmisc/aviation/aircraftsituation.h index 52dc2b384..89088cb24 100644 --- a/src/blackmisc/aviation/aircraftsituation.h +++ b/src/blackmisc/aviation/aircraftsituation.h @@ -132,8 +132,8 @@ namespace BlackMisc //! Get altitude const BlackMisc::Aviation::CAltitude &getAltitude() const { return this->m_position.geodeticHeight(); } - //! Get altitude under consideration of ground elevation and CG (if available) - BlackMisc::Aviation::CAltitude getCorrectedAltitude(const PhysicalQuantities::CLength &cgAboveGround = { 0, nullptr }) const; + //! Get altitude under consideration of ground elevation + BlackMisc::Aviation::CAltitude getCorrectedAltitude() const; //! Set altitude void setAltitude(const BlackMisc::Aviation::CAltitude &altitude) { this->m_position.setGeodeticHeight(altitude); } diff --git a/src/blackmisc/simulation/interpolator.cpp b/src/blackmisc/simulation/interpolator.cpp index 57f84bc23..36d7eff05 100644 --- a/src/blackmisc/simulation/interpolator.cpp +++ b/src/blackmisc/simulation/interpolator.cpp @@ -8,11 +8,13 @@ */ #include "interpolator.h" -#include "blackmisc/aviation/callsign.h" #include "blackmisc/simulation/interpolationhints.h" +#include "blackmisc/aviation/callsign.h" +#include "blackmisc/pq/length.h" #include using namespace BlackMisc::Aviation; +using namespace BlackMisc::PhysicalQuantities; namespace BlackMisc { @@ -68,7 +70,7 @@ namespace BlackMisc const auto soonestLanding = std::find_if(partsNewer.begin(), partsNewer.end(), [](auto && p) { return p.isOnGround(); }); // our clairvoyance is limited by the time offset - const double significantPast = 5.0; + const double significantPast = 5.0; // \fixme 20170121 KB would it make sense to centrally define the update time (5secs), in case it changes. I see a lot of 5.0 hardcoded here const double predictableFuture = soonestLanding == partsNewer.end() ? 5.0 : std::min(5.0, static_cast(soonestLanding->getTimeOffsetMs()) / 1000.0); const double secondsSinceTakeoff = latestTakeoff == partsOlder.end() ? 5.0 : (currentTimeMsSinceEpoch - latestTakeoff->getAdjustedMSecsSinceEpoch()) / 1000.0; @@ -107,10 +109,10 @@ namespace BlackMisc void IInterpolator::setGroundElevationFromHint(const CInterpolationHints &hints, CAircraftSituation &situation) { - if (hints.getElevation().isNull()) return; - if (situation.hasGroundElevation()) return; - if (!hints.isWithinRange(situation)) return; - situation.setGroundElevation(hints.getElevation().geodeticHeight()); + if (situation.hasGroundElevation()) { return; } + const CAltitude elevation = hints.getGroundElevation(situation); + if (elevation.isNull()) { return; } + situation.setGroundElevation(elevation); } bool IInterpolator::InterpolationStatus::allTrue() const diff --git a/src/blackmisc/simulation/interpolatorlinear.cpp b/src/blackmisc/simulation/interpolatorlinear.cpp index 2fc9c3d62..5407556d1 100644 --- a/src/blackmisc/simulation/interpolatorlinear.cpp +++ b/src/blackmisc/simulation/interpolatorlinear.cpp @@ -100,8 +100,8 @@ namespace BlackMisc // take hint into account to calculate elevation and above ground level if (!hints.getElevation().isNull()) { - setGroundElevationFromHint(hints, oldSituation); - setGroundElevationFromHint(hints, newSituation); + IInterpolator::setGroundElevationFromHint(hints, oldSituation); + IInterpolator::setGroundElevationFromHint(hints, newSituation); } CAircraftSituation currentSituation(oldSituation); @@ -135,8 +135,8 @@ namespace BlackMisc currentSituation.setPosition(currentPosition); // Interpolate altitude: Alt = (AltB - AltA) * t + AltA - const CAltitude oldAlt(oldSituation.getCorrectedAltitude(hints.getCGAboveGround())); - const CAltitude newAlt(newSituation.getCorrectedAltitude(hints.getCGAboveGround())); + const CAltitude oldAlt(oldSituation.getCorrectedAltitude()); // avoid underflow below ground elevation + const CAltitude newAlt(newSituation.getCorrectedAltitude()); Q_ASSERT_X(oldAlt.getReferenceDatum() == newAlt.getReferenceDatum(), Q_FUNC_INFO, "mismatch in reference"); // otherwise no calculation is possible currentSituation.setAltitude(CAltitude((newAlt - oldAlt) * simulationTimeFraction @@ -149,11 +149,12 @@ namespace BlackMisc const double groundFactor = hints.getAircraftParts().isOnGroundInterpolated(); if (groundFactor > 0.0) { - const auto &groundElevation = hints.getElevationProvider(); - if (groundElevation) + const CAltitude groundElevation = hints.getGroundElevation(currentSituation); + Q_ASSERT_X(groundElevation.getReferenceDatum() == CAltitude::MeanSeaLevel, Q_FUNC_INFO, "Need MSL value"); + if (!groundElevation.isNull()) { currentSituation.setAltitude(CAltitude(currentSituation.getAltitude() * (1.0 - groundFactor) - + groundElevation(currentSituation) * groundFactor, + + groundElevation * groundFactor, oldAlt.getReferenceDatum())); } } diff --git a/src/plugins/simulator/fsx/simulatorfsx.cpp b/src/plugins/simulator/fsx/simulatorfsx.cpp index 764451021..b5db7e3ea 100644 --- a/src/plugins/simulator/fsx/simulatorfsx.cpp +++ b/src/plugins/simulator/fsx/simulatorfsx.cpp @@ -1000,7 +1000,7 @@ namespace BlackSimPlugin SIMCONNECT_DATA_INITPOSITION position; position.Latitude = situation.latitude().value(CAngleUnit::deg()); position.Longitude = situation.longitude().value(CAngleUnit::deg()); - position.Altitude = situation.getCorrectedAltitude(hints.getCGAboveGround()).value(CLengthUnit::ft()); + position.Altitude = situation.getAltitude().value(CLengthUnit::ft()); // already corrected in interpolator if there is an underflow position.Heading = situation.getHeading().value(CAngleUnit::deg()); position.Airspeed = situation.getGroundSpeed().value(CSpeedUnit::kts());