diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index b5d8aaa86..2ba401aab 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -109,7 +109,12 @@ namespace BlackCore void ISimulator::rememberElevationAndCG(const CCallsign &callsign, const Geo::CElevationPlane &elevation, const CLength &cg) { if (callsign.isEmpty()) { return; } - if (!elevation.isNull()) { this->rememberGroundElevation(elevation); } + if (!elevation.isNull()) + { + const int aircraftCount = this->getAircraftInRangeCount(); + this->setRememberMaxElevations(aircraftCount * 3); // at least 3 elevations per aircraft, even better as not all are requesting elevations + this->rememberGroundElevation(elevation); + } if (!cg.isNull() && !this->hasSameCG(cg, callsign)) { this->insertCG(cg, callsign); } } @@ -120,9 +125,11 @@ namespace BlackCore { // decouple, follow up of signal can include unloading // simulator so this should happen strictly asyncronously (which is like forcing Qt::QueuedConnection) + QPointer myself(this); QTimer::singleShot(0, this, [ = ] { - emit this->simulatorStatusChanged(newStatus); + if (!myself) { return; } + emit myself->simulatorStatusChanged(newStatus); }); } } diff --git a/src/blackmisc/simulation/simulationenvironmentprovider.cpp b/src/blackmisc/simulation/simulationenvironmentprovider.cpp index fb8f97b5f..b51338deb 100644 --- a/src/blackmisc/simulation/simulationenvironmentprovider.cpp +++ b/src/blackmisc/simulation/simulationenvironmentprovider.cpp @@ -20,6 +20,7 @@ namespace BlackMisc bool ISimulationEnvironmentProvider::rememberGroundElevation(const ICoordinateGeodetic &elevationCoordinate, const CLength &epsilon) { { + // no 2nd elevation nearby QReadLocker l(&m_lockElvCoordinates); if (m_elvCoordinates.containsObjectInRange(elevationCoordinate, minRange(epsilon))) { return false; } } @@ -28,7 +29,7 @@ namespace BlackMisc // * we assume we find them faster // * and need the more frequently (the recent ones) QWriteLocker l(&m_lockElvCoordinates); - if (m_elvCoordinates.size() > MaxElevations) { m_elvCoordinates.pop_back(); } + if (m_elvCoordinates.size() > m_maxElevations) { m_elvCoordinates.pop_back(); } m_elvCoordinates.push_front(elevationCoordinate); } return true; @@ -76,9 +77,18 @@ namespace BlackMisc return m_elvCoordinates; } + CCoordinateGeodeticList ISimulationEnvironmentProvider::getElevationCoordinates(int &maxRemembered) const + { + QReadLocker l(&m_lockElvCoordinates); + maxRemembered = m_maxElevations; + return m_elvCoordinates; + } + int ISimulationEnvironmentProvider::cleanUpElevations(const ICoordinateGeodetic &referenceCoordinate, int maxNumber) { - CCoordinateGeodeticList coordinates(this->getElevationCoordinates()); + int currentMax; + CCoordinateGeodeticList coordinates(this->getElevationCoordinates(currentMax)); + if (maxNumber < 0) { maxNumber = currentMax; } const int size = coordinates.size(); if (size <= maxNumber) { return 0; } coordinates.sortByEuclideanDistanceSquared(referenceCoordinate); @@ -168,6 +178,19 @@ namespace BlackMisc return m_cgs[callsign] == cg; } + int ISimulationEnvironmentProvider::setRememberMaxElevations(int max) + { + QWriteLocker l(&m_lockElvCoordinates); + m_maxElevations = qMax(max, 50); + return m_maxElevations; + } + + int ISimulationEnvironmentProvider::getRememberMaxElevations() const + { + QReadLocker l(&m_lockElvCoordinates); + return m_maxElevations; + } + ISimulationEnvironmentProvider::ISimulationEnvironmentProvider(const CSimulatorPluginInfo &pluginInfo) : m_simulatorPluginInfo(pluginInfo) { } diff --git a/src/blackmisc/simulation/simulationenvironmentprovider.h b/src/blackmisc/simulation/simulationenvironmentprovider.h index 39fb31320..5fcda758c 100644 --- a/src/blackmisc/simulation/simulationenvironmentprovider.h +++ b/src/blackmisc/simulation/simulationenvironmentprovider.h @@ -33,7 +33,6 @@ namespace BlackMisc { public: //! All remembered coordiantes - //! \remark might be a relatively large list //! \threadsafe Geo::CCoordinateGeodeticList getElevationCoordinates() const; @@ -77,10 +76,22 @@ namespace BlackMisc //! \threadsafe bool hasSameCG(const PhysicalQuantities::CLength &cg, const Aviation::CCallsign &callsign) const; + //! Set number of elevations kept + //! \threadsafe + int setRememberMaxElevations(int max); + + //! Get number of max. number of elevations + //! \threadsafe + int getRememberMaxElevations() const; + protected: //! Ctor ISimulationEnvironmentProvider(const CSimulatorPluginInfo &pluginInfo); + //! All remembered coordiantes plus max remembered situations + //! \threadsafe + Geo::CCoordinateGeodeticList getElevationCoordinates(int &maxRemembered) const; + //! New plugin info and default model //! \remark normally only used by emulated driver //! \threadsafe @@ -107,7 +118,7 @@ namespace BlackMisc //! Only keep closest ones //! \threadsafe - int cleanUpElevations(const Geo::ICoordinateGeodetic &referenceCoordinate, int maxNumber = MaxElevations); + int cleanUpElevations(const Geo::ICoordinateGeodetic &referenceCoordinate, int maxNumber = -1); //! Remember a given elevation //! \threadsafe @@ -129,9 +140,8 @@ namespace BlackMisc //! Min.range considered as single point static PhysicalQuantities::CLength minRange(const PhysicalQuantities::CLength &range); - static constexpr int MaxElevations = 1000; //!< How many elevations we keep - private: + int m_maxElevations = 100; //!< How many elevations we keep CAircraftModel m_defaultModel; //!< default model CSimulatorPluginInfo m_simulatorPluginInfo; //!< info object Geo::CCoordinateGeodeticList m_elvCoordinates;