From 2ccd9bfb3038170af6bf37d10d5dd6fd452e7422 Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Tue, 5 Apr 2022 11:47:02 +0200 Subject: [PATCH] [swift] Pass struct to updatePlanes() instead of multiple vectors --- src/AIModel/AISwiftAircraft.cpp | 2 +- src/AIModel/AISwiftAircraft.h | 2 +- src/Network/Swift/SwiftAircraftManager.cpp | 8 ++++---- src/Network/Swift/SwiftAircraftManager.h | 10 +++++++++- src/Network/Swift/traffic.cpp | 19 ++++++++----------- .../Network/test_swiftAircraftManager.cxx | 6 +++--- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/AIModel/AISwiftAircraft.cpp b/src/AIModel/AISwiftAircraft.cpp index 0fb0212fa..cf09faf28 100755 --- a/src/AIModel/AISwiftAircraft.cpp +++ b/src/AIModel/AISwiftAircraft.cpp @@ -33,7 +33,7 @@ FGAISwiftAircraft::FGAISwiftAircraft(const std::string& callsign, const std::str _searchOrder = ModelSearchOrder::PREFER_AI; } -void FGAISwiftAircraft::updatePosition(const SGGeod &position, const SGVec3 &orientation, double groundspeed, bool initPos) +void FGAISwiftAircraft::updatePosition(const SGGeod &position, const SGVec3d &orientation, double groundspeed, bool initPos) { m_initPos = initPos; _setLatitude(position.getLatitudeDeg()); diff --git a/src/AIModel/AISwiftAircraft.h b/src/AIModel/AISwiftAircraft.h index 3a10d28a0..7c1e88bfd 100644 --- a/src/AIModel/AISwiftAircraft.h +++ b/src/AIModel/AISwiftAircraft.h @@ -74,7 +74,7 @@ public: string_view getTypeString() const override { return "swift"; } void update(double dt) override; - void updatePosition(const SGGeod &position, const SGVec3 &orientation, double groundspeed, bool initPos); + void updatePosition(const SGGeod &position, const SGVec3d &orientation, double groundspeed, bool initPos); double getGroundElevation(const SGGeod& pos) const; void initProps(); void setPlaneSurface(const AircraftSurfaces& surfaces); diff --git a/src/Network/Swift/SwiftAircraftManager.cpp b/src/Network/Swift/SwiftAircraftManager.cpp index 3f5434679..e9bb613da 100644 --- a/src/Network/Swift/SwiftAircraftManager.cpp +++ b/src/Network/Swift/SwiftAircraftManager.cpp @@ -37,12 +37,12 @@ bool FGSwiftAircraftManager::addPlane(const std::string& callsign, const std::st return true; } -void FGSwiftAircraftManager::updatePlanes(const std::vector& callsigns, const std::vector& positions, const std::vector& orientations, const std::vector& groundspeeds, const std::vector& onGrounds) +void FGSwiftAircraftManager::updatePlanes(const std::vector& updates) { - for (long unsigned int i = 0; i < callsigns.size(); i++) { - auto it = aircraftByCallsign.find(callsigns.at(i)); + for (auto& update : updates) { + auto it = aircraftByCallsign.find(update.callsign); if (it != aircraftByCallsign.end()) { - it->second->updatePosition(positions.at(i), orientations.at(i), groundspeeds.at(i), true); + it->second->updatePosition(update.position, update.orientation, update.groundspeed, true); } } } diff --git a/src/Network/Swift/SwiftAircraftManager.h b/src/Network/Swift/SwiftAircraftManager.h index 255209dfe..3bfafb715 100644 --- a/src/Network/Swift/SwiftAircraftManager.h +++ b/src/Network/Swift/SwiftAircraftManager.h @@ -15,6 +15,14 @@ #ifndef FGSWIFTAIRCRAFTMANAGER_H #define FGSWIFTAIRCRAFTMANAGER_H +struct SwiftPlaneUpdate { + std::string callsign; + SGGeod position; + SGVec3d orientation; + double groundspeed; + bool onGround; +}; + class FGSwiftAircraftManager { using FGAISwiftAircraftPtr = SGSharedPtr; @@ -23,7 +31,7 @@ public: FGSwiftAircraftManager(); ~FGSwiftAircraftManager(); bool addPlane(const std::string& callsign, const std::string& modelString); - void updatePlanes(const std::vector& callsigns, const std::vector& positions, const std::vector& orientations, const std::vector& groundspeeds, const std::vector& onGrounds); + void updatePlanes(const std::vector& updates); void getRemoteAircraftData(std::vector& callsigns, std::vector& latitudesDeg, std::vector& longitudesDeg, std::vector& elevationsM, std::vector& verticalOffsets) const; void removePlane(const std::string& callsign); diff --git a/src/Network/Swift/traffic.cpp b/src/Network/Swift/traffic.cpp index 5bf12ccfa..fda4987c6 100644 --- a/src/Network/Swift/traffic.cpp +++ b/src/Network/Swift/traffic.cpp @@ -161,19 +161,16 @@ DBusHandlerResult CTraffic::dbusMessageHandler(const CDBusMessage& message_) message.getArgument(groundspeeds); message.getArgument(onGrounds); queueDBusCall([=]() { - std::vector positions; - std::vector orientations; + std::vector updates; for (long unsigned int i = 0; i < latitudes.size(); i++) { - SGGeod newPos; - newPos.setLatitudeDeg(latitudes.at(i)); - newPos.setLongitudeDeg(longitudes.at(i)); - newPos.setElevationFt(altitudes.at(i)); - SGVec3d vec(pitches.at(i), rolls.at(i), headings.at(i)); - - positions.push_back(newPos); - orientations.push_back(vec); + SGGeod pos; + pos.setLatitudeDeg(latitudes.at(i)); + pos.setLongitudeDeg(longitudes.at(i)); + pos.setElevationFt(altitudes.at(i)); + SGVec3d orientation(pitches.at(i), rolls.at(i), headings.at(i)); + updates.push_back({callsigns.at(i), pos, orientation, groundspeeds.at(i), onGrounds.at(i)}); } - acm->updatePlanes(callsigns, positions, orientations, groundspeeds, onGrounds); + acm->updatePlanes(updates); }); } else if (message.getMethodName() == "getRemoteAircraftData") { std::vector requestedcallsigns; diff --git a/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx b/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx index f7f474f9d..daee225c8 100644 --- a/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx +++ b/test_suite/unit_tests/Network/test_swiftAircraftManager.cxx @@ -61,7 +61,7 @@ void SwiftAircraftManagerTest::testAircraftManager() position.setLongitudeDeg(6.0); position.setElevationM(1024); - acm.updatePlanes({"SAS123"}, {position}, {SGVec3d(1.0, 2.0, 3.0)}, {200}, {false}); + acm.updatePlanes({{"SAS123", position, SGVec3d(1.0, 2.0, 3.0), 200, false}}); CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[3]/callsign"), std::string("SAS123")); CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/pitch-deg"), 1.0, 0.1); CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/roll-deg"), 2.0, 0.1); @@ -72,7 +72,7 @@ void SwiftAircraftManagerTest::testAircraftManager() position.setLatitudeDeg(20.0); position.setLongitudeDeg(4.0); - acm.updatePlanes({"SAS123"}, {position}, {SGVec3d(5.0, 6.0, 7.0)}, {400}, {false}); + acm.updatePlanes({{"SAS123", position, SGVec3d(5.0, 6.0, 7.0), 400, false}}); CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[3]/callsign"), std::string("SAS123")); CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/pitch-deg"), 5.0, 0.1); CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[3]/orientation/roll-deg"), 6.0, 0.1); @@ -86,7 +86,7 @@ void SwiftAircraftManagerTest::testAircraftManager() acm.addPlane("DAL123", "PATH_TO_MODEL"); position.setLatitudeDeg(-20.0); position.setLongitudeDeg(5.0); - acm.updatePlanes({"DAL123"}, {position}, {SGVec3d(1.0, 1.0, 1.0)}, {250}, {false}); + acm.updatePlanes({{"DAL123", position, SGVec3d(1.0, 1.0, 1.0), 250, false}}); CPPUNIT_ASSERT_EQUAL(fgGetString("/ai/models/swift[4]/callsign"), std::string("DAL123")); CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/orientation/pitch-deg"), 1.0, 0.1); CPPUNIT_ASSERT_DOUBLES_EQUAL(fgGetDouble("/ai/models/swift[4]/orientation/roll-deg"), 1.0, 0.1);