From 90c8c0183aa25909d6f36ff0de5a66c2c39071c1 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 19 Feb 2023 15:16:34 -0600 Subject: [PATCH] USe a common default geodesic object instead of duplicate copies. All used functions are constant, and don't require unique instantiations for thread safety. --- scwx-qt/scwx-qt.cmake | 2 ++ scwx-qt/source/scwx/qt/config/radar_site.cpp | 15 +++++------- scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp | 8 +++---- .../scwx/qt/manager/radar_product_manager.cpp | 24 +++++++++---------- .../source/scwx/qt/map/radar_range_layer.cpp | 6 ++--- scwx-qt/source/scwx/qt/model/alert_model.cpp | 15 ++++++------ .../source/scwx/qt/model/radar_site_model.cpp | 8 +++---- .../source/scwx/qt/util/geographic_lib.cpp | 24 +++++++++++++++++++ .../source/scwx/qt/util/geographic_lib.hpp | 24 +++++++++++++++++++ .../scwx/qt/view/level3_raster_view.cpp | 10 ++++---- 10 files changed, 89 insertions(+), 47 deletions(-) create mode 100644 scwx-qt/source/scwx/qt/util/geographic_lib.cpp create mode 100644 scwx-qt/source/scwx/qt/util/geographic_lib.hpp diff --git a/scwx-qt/scwx-qt.cmake b/scwx-qt/scwx-qt.cmake index 3d936401..9609ddf0 100644 --- a/scwx-qt/scwx-qt.cmake +++ b/scwx-qt/scwx-qt.cmake @@ -163,6 +163,7 @@ set(HDR_UTIL source/scwx/qt/util/color.hpp source/scwx/qt/util/file.hpp source/scwx/qt/util/font.hpp source/scwx/qt/util/font_buffer.hpp + source/scwx/qt/util/geographic_lib.hpp source/scwx/qt/util/json.hpp source/scwx/qt/util/streams.hpp source/scwx/qt/util/texture_atlas.hpp @@ -172,6 +173,7 @@ set(SRC_UTIL source/scwx/qt/util/color.cpp source/scwx/qt/util/file.cpp source/scwx/qt/util/font.cpp source/scwx/qt/util/font_buffer.cpp + source/scwx/qt/util/geographic_lib.cpp source/scwx/qt/util/json.cpp source/scwx/qt/util/texture_atlas.cpp source/scwx/qt/util/q_file_buffer.cpp diff --git a/scwx-qt/source/scwx/qt/config/radar_site.cpp b/scwx-qt/source/scwx/qt/config/radar_site.cpp index 5d2851f8..6feadde9 100644 --- a/scwx-qt/source/scwx/qt/config/radar_site.cpp +++ b/scwx-qt/source/scwx/qt/config/radar_site.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -8,7 +9,6 @@ #include #include -#include namespace scwx { @@ -31,9 +31,6 @@ static std::unordered_map> static std::unordered_map siteIdMap_; static std::shared_mutex siteMutex_; -static GeographicLib::Geodesic geodesic_ {GeographicLib::Constants::WGS84_a(), - GeographicLib::Constants::WGS84_f()}; - static bool ValidateJsonEntry(const boost::json::object& o); class RadarSiteImpl @@ -187,11 +184,11 @@ std::shared_ptr RadarSite::FindNearest( } // Calculate distance to radar site - geodesic_.Inverse(latitude, - longitude, - radarSite->latitude(), - radarSite->longitude(), - distanceInMeters); + util::GeographicLib::DefaultGeodesic().Inverse(latitude, + longitude, + radarSite->latitude(), + radarSite->longitude(), + distanceInMeters); // If the radar site is the closer, record it as the closest if (nearestRadarSite == nullptr || distanceInMeters < nearestDistance) diff --git a/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp b/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp index 04e57048..39567a2d 100644 --- a/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp +++ b/scwx-qt/source/scwx/qt/gl/draw/geo_line.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,8 +7,6 @@ #include #include -#include - namespace scwx { namespace qt @@ -33,8 +32,7 @@ class GeoLine::Impl public: explicit Impl(std::shared_ptr context) : context_ {context}, - geodesic_(GeographicLib::Constants::WGS84_a(), - GeographicLib::Constants::WGS84_f()), + geodesic_ {util::GeographicLib::DefaultGeodesic()}, dirty_ {false}, visible_ {true}, points_ {}, @@ -55,7 +53,7 @@ public: std::shared_ptr context_; - GeographicLib::Geodesic geodesic_; + const GeographicLib::Geodesic& geodesic_; bool dirty_; diff --git a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp index e7f1192f..80514e49 100644 --- a/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp +++ b/scwx-qt/source/scwx/qt/manager/radar_product_manager.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -18,7 +19,6 @@ #include #include #include -#include #include #pragma warning(pop) @@ -81,7 +81,7 @@ public: group_ {group}, product_ {product}, refreshEnabled_ {false}, - refreshTimer_ {util::io_context()}, + refreshTimer_ {scwx::util::io_context()}, refreshTimerMutex_ {}, provider_ {nullptr} { @@ -303,8 +303,8 @@ void RadarProductManager::Initialize() boost::timer::cpu_timer timer; - GeographicLib::Geodesic geodesic(GeographicLib::Constants::WGS84_a(), - GeographicLib::Constants::WGS84_f()); + const GeographicLib::Geodesic& geodesic( + util::GeographicLib::DefaultGeodesic()); const QMapLibreGL::Coordinate radar(p->radarSite_->latitude(), p->radarSite_->longitude()); @@ -425,7 +425,7 @@ void RadarProductManager::EnableRefresh(common::RadarProductGroup group, p->GetLevel3ProviderManager(product); // Only enable refresh on available products - util::async( + scwx::util::async( [=]() { providerManager->provider_->RequestAvailableProducts(); @@ -467,7 +467,7 @@ void RadarProductManagerImpl::RefreshData( providerManager->refreshTimer_.cancel(); } - util::async( + scwx::util::async( [=]() { auto [newObjects, totalObjects] = @@ -547,7 +547,7 @@ void RadarProductManagerImpl::LoadProviderData( { logger_->debug("LoadProviderData: {}, {}", providerManager->name(), - util::TimeString(time)); + scwx::util::TimeString(time)); RadarProductManagerImpl::LoadNexradFile( [=, &recordMap, &recordMutex, &loadDataMutex]() @@ -589,7 +589,7 @@ void RadarProductManager::LoadLevel2Data( std::chrono::system_clock::time_point time, std::shared_ptr request) { - logger_->debug("LoadLevel2Data: {}", util::TimeString(time)); + logger_->debug("LoadLevel2Data: {}", scwx::util::TimeString(time)); p->LoadProviderData(time, p->level2ProviderManager_, @@ -604,7 +604,7 @@ void RadarProductManager::LoadLevel3Data( std::chrono::system_clock::time_point time, std::shared_ptr request) { - logger_->debug("LoadLevel3Data: {}", util::TimeString(time)); + logger_->debug("LoadLevel3Data: {}", scwx::util::TimeString(time)); // Look up provider manager std::shared_lock providerManagerLock(p->level3ProviderManagerMutex_); @@ -742,7 +742,7 @@ RadarProductManagerImpl::GetLevel2ProductRecord( else { // TODO: Round to minutes - record = util::GetBoundedElementValue(level2ProductRecords_, time); + record = scwx::util::GetBoundedElementValue(level2ProductRecords_, time); // Does the record contain the time we are looking for? if (record != nullptr && (time < record->level2_file()->start_time())) @@ -774,7 +774,7 @@ RadarProductManagerImpl::GetLevel3ProductRecord( } else { - record = util::GetBoundedElementValue(it->second, time); + record = scwx::util::GetBoundedElementValue(it->second, time); } } @@ -904,7 +904,7 @@ void RadarProductManager::UpdateAvailableProducts() logger_->debug("UpdateAvailableProducts()"); - util::async( + scwx::util::async( [=]() { auto level3ProviderManager = diff --git a/scwx-qt/source/scwx/qt/map/radar_range_layer.cpp b/scwx-qt/source/scwx/qt/map/radar_range_layer.cpp index 545d0880..ea4fa55b 100644 --- a/scwx-qt/source/scwx/qt/map/radar_range_layer.cpp +++ b/scwx-qt/source/scwx/qt/map/radar_range_layer.cpp @@ -1,7 +1,7 @@ #include +#include #include -#include #include namespace scwx @@ -61,8 +61,8 @@ void RadarRangeLayer::Update(std::shared_ptr map, static std::shared_ptr GetRangeCircle(float range, QMapLibreGL::Coordinate center) { - GeographicLib::Geodesic geodesic(GeographicLib::Constants::WGS84_a(), - GeographicLib::Constants::WGS84_f()); + const GeographicLib::Geodesic& geodesic( + util::GeographicLib::DefaultGeodesic()); constexpr float angleDelta = 0.5f; constexpr float angleDeltaH = angleDelta / 2.0f; diff --git a/scwx-qt/source/scwx/qt/model/alert_model.cpp b/scwx-qt/source/scwx/qt/model/alert_model.cpp index a2213988..03d3119a 100644 --- a/scwx-qt/source/scwx/qt/model/alert_model.cpp +++ b/scwx-qt/source/scwx/qt/model/alert_model.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -9,7 +10,6 @@ #include -#include #include #include @@ -47,7 +47,7 @@ public: QList textEventKeys_; - GeographicLib::Geodesic geodesic_; + const GeographicLib::Geodesic& geodesic_; std::unordered_mapsegment_count(); auto lastSegment = lastMessage->segment(segmentCount - 1); - return util::ToString(lastSegment->header_->ugc_.states()); + return scwx::util::ToString(lastSegment->header_->ugc_.states()); } std::chrono::system_clock::time_point @@ -404,7 +403,7 @@ AlertModelImpl::GetStartTime(const types::TextEventKey& key) std::string AlertModelImpl::GetStartTimeString(const types::TextEventKey& key) { - return util::TimeString(GetStartTime(key)); + return scwx::util::TimeString(GetStartTime(key)); } std::chrono::system_clock::time_point @@ -419,7 +418,7 @@ AlertModelImpl::GetEndTime(const types::TextEventKey& key) std::string AlertModelImpl::GetEndTimeString(const types::TextEventKey& key) { - return util::TimeString(GetEndTime(key)); + return scwx::util::TimeString(GetEndTime(key)); } } // namespace model diff --git a/scwx-qt/source/scwx/qt/model/radar_site_model.cpp b/scwx-qt/source/scwx/qt/model/radar_site_model.cpp index 6e9b321c..bc059953 100644 --- a/scwx-qt/source/scwx/qt/model/radar_site_model.cpp +++ b/scwx-qt/source/scwx/qt/model/radar_site_model.cpp @@ -1,13 +1,12 @@ #include #include #include +#include #include #include #include -#include - namespace scwx { namespace qt @@ -36,7 +35,7 @@ public: QList> radarSites_; - GeographicLib::Geodesic geodesic_; + const GeographicLib::Geodesic& geodesic_; std::unordered_map distanceMap_; scwx::common::DistanceType distanceDisplay_; @@ -186,8 +185,7 @@ void RadarSiteModel::HandleMapUpdate(double latitude, double longitude) RadarSiteModelImpl::RadarSiteModelImpl() : radarSites_ {}, - geodesic_(GeographicLib::Constants::WGS84_a(), - GeographicLib::Constants::WGS84_f()), + geodesic_(util::GeographicLib::DefaultGeodesic()), distanceMap_ {}, distanceDisplay_ {scwx::common::DistanceType::Miles}, previousPosition_ {} diff --git a/scwx-qt/source/scwx/qt/util/geographic_lib.cpp b/scwx-qt/source/scwx/qt/util/geographic_lib.cpp new file mode 100644 index 00000000..5c55b9e2 --- /dev/null +++ b/scwx-qt/source/scwx/qt/util/geographic_lib.cpp @@ -0,0 +1,24 @@ +#include + +namespace scwx +{ +namespace qt +{ +namespace util +{ +namespace GeographicLib +{ + +const ::GeographicLib::Geodesic& DefaultGeodesic() +{ + static const ::GeographicLib::Geodesic geodesic_ { + ::GeographicLib::Constants::WGS84_a(), + ::GeographicLib::Constants::WGS84_f()}; + + return geodesic_; +} + +} // namespace GeographicLib +} // namespace util +} // namespace qt +} // namespace scwx diff --git a/scwx-qt/source/scwx/qt/util/geographic_lib.hpp b/scwx-qt/source/scwx/qt/util/geographic_lib.hpp new file mode 100644 index 00000000..3fe3c187 --- /dev/null +++ b/scwx-qt/source/scwx/qt/util/geographic_lib.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +namespace scwx +{ +namespace qt +{ +namespace util +{ +namespace GeographicLib +{ + +/** + * Get the default geodesic for the WGS84 ellipsoid. + * + * return WGS84 ellipsoid geodesic + */ +const ::GeographicLib::Geodesic& DefaultGeodesic(); + +} // namespace GeographicLib +} // namespace util +} // namespace qt +} // namespace scwx diff --git a/scwx-qt/source/scwx/qt/view/level3_raster_view.cpp b/scwx-qt/source/scwx/qt/view/level3_raster_view.cpp index d606a581..84c896ae 100644 --- a/scwx-qt/source/scwx/qt/view/level3_raster_view.cpp +++ b/scwx-qt/source/scwx/qt/view/level3_raster_view.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,7 +8,6 @@ #include #include -#include namespace scwx { @@ -202,12 +202,12 @@ void Level3RasterView::ComputeSweep() p->longitude_ = descriptionBlock->longitude_of_radar(); p->range_ = descriptionBlock->range(); p->sweepTime_ = - util::TimePoint(descriptionBlock->volume_scan_date(), - descriptionBlock->volume_scan_start_time() * 1000); + scwx::util::TimePoint(descriptionBlock->volume_scan_date(), + descriptionBlock->volume_scan_start_time() * 1000); p->vcp_ = descriptionBlock->volume_coverage_pattern(); - GeographicLib::Geodesic geodesic(GeographicLib::Constants::WGS84_a(), - GeographicLib::Constants::WGS84_f()); + const GeographicLib::Geodesic& geodesic = + util::GeographicLib::DefaultGeodesic(); const uint16_t xResolution = descriptionBlock->x_resolution_raw(); const uint16_t yResolution = descriptionBlock->y_resolution_raw();