From 3346a92b5687537ec35d1d6ad184dbf4843102e8 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 7 Mar 2018 02:04:21 +0100 Subject: [PATCH] Ref T259, Ref T243 simulator environment provider * access to elevation * future: more data such as airport info --- src/blackcore/airspacemonitor.h | 16 ++-- .../simulationenvironmentprovider.cpp | 73 ++++++++++++++++ .../simulationenvironmentprovider.h | 83 +++++++++++++++++++ .../fsxcommon/simulatorfsxcommon.cpp | 3 + .../simulator/xplane/simulatorxplane.cpp | 1 + 5 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 src/blackmisc/simulation/simulationenvironmentprovider.cpp create mode 100644 src/blackmisc/simulation/simulationenvironmentprovider.h diff --git a/src/blackcore/airspacemonitor.h b/src/blackcore/airspacemonitor.h index 44194d014..24a73c6dc 100644 --- a/src/blackcore/airspacemonitor.h +++ b/src/blackcore/airspacemonitor.h @@ -25,6 +25,13 @@ #include "blackcore/blackcoreexport.h" #include "blackcore/network.h" +#include "blackmisc/simulation/aircraftmodel.h" +#include "blackmisc/simulation/airspaceaircraftsnapshot.h" +#include "blackmisc/simulation/ownaircraftprovider.h" +#include "blackmisc/simulation/remoteaircraftprovider.h" +#include "blackmisc/simulation/simulationenvironmentprovider.h" +#include "blackmisc/simulation/simulatedaircraft.h" +#include "blackmisc/simulation/simulatedaircraftlist.h" #include "blackmisc/aviation/aircraftpartslist.h" #include "blackmisc/aviation/aircraftsituationlist.h" #include "blackmisc/aviation/atcstation.h" @@ -40,12 +47,6 @@ #include "blackmisc/pq/frequency.h" #include "blackmisc/pq/length.h" #include "blackmisc/pq/angle.h" -#include "blackmisc/simulation/aircraftmodel.h" -#include "blackmisc/simulation/airspaceaircraftsnapshot.h" -#include "blackmisc/simulation/ownaircraftprovider.h" -#include "blackmisc/simulation/remoteaircraftprovider.h" -#include "blackmisc/simulation/simulatedaircraft.h" -#include "blackmisc/simulation/simulatedaircraftlist.h" namespace BlackMisc { @@ -69,7 +70,8 @@ namespace BlackCore class BLACKCORE_EXPORT CAirspaceMonitor : public QObject, public BlackMisc::Simulation::IRemoteAircraftProvider, // those data will be provided from the class CAirspaceMonitor - public BlackMisc::Simulation::COwnAircraftAware, // used to obtain in memory information about own aircraft + public BlackMisc::Simulation::COwnAircraftAware, // used to obtain in memory information about own aircraft + public BlackMisc::Simulation::CSimulationEnvironmentAware, // elevation info etc. public BlackMisc::CIdentifiable { Q_OBJECT diff --git a/src/blackmisc/simulation/simulationenvironmentprovider.cpp b/src/blackmisc/simulation/simulationenvironmentprovider.cpp new file mode 100644 index 000000000..89ace4cf1 --- /dev/null +++ b/src/blackmisc/simulation/simulationenvironmentprovider.cpp @@ -0,0 +1,73 @@ +/* Copyright (C) 2018 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "simulationenvironmentprovider.h" + +using namespace BlackMisc::Geo; + +namespace BlackMisc +{ + namespace Simulation + { + bool ISimulationEnvironmentProvider::rememberGroundElevation(const ICoordinateGeodetic &elevationCoordinate, const PhysicalQuantities::CLength &epsilon) + { + { + QReadLocker l(&m_lockElvCoordinates); + if (m_elvCoordinates.containsObjectInRange(elevationCoordinate, epsilon)) { return false; } + } + { + QWriteLocker l(&m_lockElvCoordinates); + m_elvCoordinates.push_back(elevationCoordinate); + } + return true; + } + + bool ISimulationEnvironmentProvider::rememberGroundElevation(const CElevationPlane &elevationPlane) + { + if (!elevationPlane.hasMSLGeodeticHeight()) { return false; } + return this->rememberGroundElevation(elevationPlane, elevationPlane.getRadius()); + } + + CCoordinateGeodeticList ISimulationEnvironmentProvider::getElevationCoordinates() const + { + QReadLocker l(&m_lockElvCoordinates); + return m_elvCoordinates; + } + + int ISimulationEnvironmentProvider::cleanUpElevations(const ICoordinateGeodetic &referenceCoordinate, int maxNumber) + { + CCoordinateGeodeticList coordinates(this->getElevationCoordinates()); + const int size = coordinates.size(); + if (size <= maxNumber) { return 0; } + coordinates.sortByEuclideanDistanceSquared(referenceCoordinate); + coordinates.truncate(maxNumber); + const int delta = size - coordinates.size(); + QWriteLocker l(&m_lockElvCoordinates); + m_elvCoordinates = coordinates; + return delta; + } + + CElevationPlane ISimulationEnvironmentProvider::findClosestElevationWithinRange(const ICoordinateGeodetic &reference, const PhysicalQuantities::CLength &range) + { + return this->getElevationCoordinates().findClosestWithinRange(reference, range); + } + + void ISimulationEnvironmentProvider::clearSimulationEnvironmentData() + { + QWriteLocker l(&m_lockElvCoordinates); + m_elvCoordinates.clear(); + } + + CElevationPlane CSimulationEnvironmentAware::findClosestElevationWithinRange(const ICoordinateGeodetic &reference, const PhysicalQuantities::CLength &range) + { + if (!m_simEnvironmentProvider) { return CElevationPlane::null(); } + return m_simEnvironmentProvider->findClosestElevationWithinRange(reference, range); + } + } // namespace +} // namespace diff --git a/src/blackmisc/simulation/simulationenvironmentprovider.h b/src/blackmisc/simulation/simulationenvironmentprovider.h new file mode 100644 index 000000000..d6aa6b611 --- /dev/null +++ b/src/blackmisc/simulation/simulationenvironmentprovider.h @@ -0,0 +1,83 @@ +/* Copyright (C) 2018 + * swift project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_SIMULATION_SIMULATIONENVIRONMENTPROVIDER_H +#define BLACKMISC_SIMULATION_SIMULATIONENVIRONMENTPROVIDER_H + +#include "blackmisc/geo/coordinategeodeticlist.h" +#include "blackmisc/geo/elevationplane.h" + +namespace BlackMisc +{ + namespace Simulation + { + //! Direct in memory access to elevation data + //! \remark we are interested in elevations of airports + class BLACKMISC_EXPORT ISimulationEnvironmentProvider + { + public: + //! All remembered coordiantes + //! \remark might be a relatively large list + //! \threadsafe + Geo::CCoordinateGeodeticList getElevationCoordinates() const; + + //! Only keep closest ones + //! \threadsafe + int cleanUpElevations(const Geo::ICoordinateGeodetic &referenceCoordinate, int maxNumber = MaxElevations); + + //! Find closest elevation + //! \threadsafe + Geo::CElevationPlane findClosestElevationWithinRange(const Geo::ICoordinateGeodetic &reference, const PhysicalQuantities::CLength &range); + + //! Clear data + //! \threadsafe + void clearSimulationEnvironmentData(); + + protected: + static constexpr int MaxElevations = 1000; //!< How many elevations we keep + + //! Remember a given elevation + //! \threadsafe + bool rememberGroundElevation(const Geo::ICoordinateGeodetic &elevationCoordinate, const PhysicalQuantities::CLength &epsilon = Geo::CElevationPlane::singlePointRadius()) ; + + //! Remember a given elevation + //! \threadsafe + bool rememberGroundElevation(const Geo::CElevationPlane &elevationPlane) ; + + private: + Geo::CCoordinateGeodeticList m_elvCoordinates; + mutable QReadWriteLock m_lockElvCoordinates; //!< lock m_coordinates + }; + + //! Class which can be directly used to access an \sa ISimulationEnvironmentProvider object + class BLACKMISC_EXPORT CSimulationEnvironmentAware + { + public: + //! Elevations, can be changed over time as it comes from simulator + void setSimulationEnvironmentProvider(ISimulationEnvironmentProvider *provider) { m_simEnvironmentProvider = provider; } + + //! Find closest elevation + Geo::CElevationPlane findClosestElevationWithinRange(const Geo::ICoordinateGeodetic &reference, const PhysicalQuantities::CLength &range); + + protected: + //! Default constructor + CSimulationEnvironmentAware() {} + + //! Constructor + CSimulationEnvironmentAware(ISimulationEnvironmentProvider *simEnvProvider) : m_simEnvironmentProvider(simEnvProvider) { Q_ASSERT(simEnvProvider); } + ISimulationEnvironmentProvider *m_simEnvironmentProvider = nullptr; //!< access to object + }; + } // namespace +} // namespace + +Q_DECLARE_INTERFACE(BlackMisc::Simulation::ISimulationEnvironmentProvider, "BlackMisc::Simulation::ISimulationEnvironmentProvider") + +#endif // guard diff --git a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp index 25ea3f2f4..d58674d4f 100644 --- a/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp +++ b/src/plugins/simulator/fsxcommon/simulatorfsxcommon.cpp @@ -496,6 +496,9 @@ namespace BlackSimPlugin // set it in the remote aircraft provider this->updateAircraftGroundElevation(simObject.getCallsign(), elevation); + + // and in elevation provider + this->rememberGroundElevation(elevation); } void CSimulatorFsxCommon::updateOwnAircraftFromSimulator(const DataDefinitionClientAreaSb &sbDataArea) diff --git a/src/plugins/simulator/xplane/simulatorxplane.cpp b/src/plugins/simulator/xplane/simulatorxplane.cpp index 1469bd07e..a48d4e0df 100644 --- a/src/plugins/simulator/xplane/simulatorxplane.cpp +++ b/src/plugins/simulator/xplane/simulatorxplane.cpp @@ -1027,6 +1027,7 @@ namespace BlackSimPlugin // set it in the remote aircraft provider this->updateAircraftGroundElevation(cs, elevation); + this->rememberGroundElevation(elevation); } BlackCore::ISimulator *CSimulatorXPlaneFactory::create(const CSimulatorPluginInfo &info,