From 24950993d6ae35e979ee97ea034a32674e565872 Mon Sep 17 00:00:00 2001 From: MET Tools Test Account Date: Fri, 13 Dec 2024 20:16:48 +0000 Subject: [PATCH] Per #3012, changes to the vx_grid library to address SonarQube code smells in New Code and hopefully reduce the Overall number of code smells. --- src/libcode/vx_grid/grid_base.cc | 80 ++++++++++--------- src/libcode/vx_grid/unstructured_grid.cc | 82 ++++++-------------- src/libcode/vx_grid/unstructured_grid.h | 5 -- src/libcode/vx_grid/unstructured_grid_defs.h | 6 +- 4 files changed, 68 insertions(+), 105 deletions(-) diff --git a/src/libcode/vx_grid/grid_base.cc b/src/libcode/vx_grid/grid_base.cc index f16ee138a..4592dc19b 100644 --- a/src/libcode/vx_grid/grid_base.cc +++ b/src/libcode/vx_grid/grid_base.cc @@ -337,7 +337,7 @@ void UnstructuredData::dump() const { mlog << Debug(grid_debug_level) << "\nUnstructured Grid Data:\n" << " n_face: " << n_face << "\n" - << " points: " << (0 < points_lonlat.size() ? "PointLonLat" : "PointXYZ") << "\n" + << " points: " << (points_lonlat.empty() ? "PointXYZ" : "PointLonLat") << "\n" << " lat_checksum: " << lat_checksum << "\n" << " lon_checksum: " << lon_checksum << "\n" << " alt_checksum: " << alt_checksum << "\n" @@ -443,18 +443,18 @@ void GridInfo::clear() { -if ( lc ) { delete lc; lc = (const LambertData *) nullptr; }; -if ( st ) { delete st; st = (const StereographicData *) nullptr; }; -if ( ll ) { delete ll; ll = (const LatLonData *) nullptr; }; -if ( rll ) { delete rll; rll = (const RotatedLatLonData *) nullptr; }; -if ( m ) { delete m; m = (const MercatorData *) nullptr; }; -if ( g ) { delete g; g = (const GaussianData *) nullptr; }; -if ( gi ) { delete gi; gi = (const GoesImagerData *) nullptr; }; -if ( la ) { delete la; la = (const LaeaData *) nullptr; }; -if ( tc ) { delete tc; tc = (const TcrmwData *) nullptr; }; -if ( sl ) { delete sl; sl = (const SemiLatLonData *) nullptr; }; +if ( lc ) { delete lc; lc = (const LambertData *) nullptr; } +if ( st ) { delete st; st = (const StereographicData *) nullptr; } +if ( ll ) { delete ll; ll = (const LatLonData *) nullptr; } +if ( rll ) { delete rll; rll = (const RotatedLatLonData *) nullptr; } +if ( m ) { delete m; m = (const MercatorData *) nullptr; } +if ( g ) { delete g; g = (const GaussianData *) nullptr; } +if ( gi ) { delete gi; gi = (const GoesImagerData *) nullptr; } +if ( la ) { delete la; la = (const LaeaData *) nullptr; } +if ( tc ) { delete tc; tc = (const TcrmwData *) nullptr; } +if ( sl ) { delete sl; sl = (const SemiLatLonData *) nullptr; } #ifdef WITH_UGRID -if ( us ) { delete us; us = (const UnstructuredData *) nullptr; }; +if ( us ) { delete us; us = (const UnstructuredData *) nullptr; } #endif return; @@ -556,13 +556,13 @@ void GridInfo::set(const LambertData & data) clear(); -LambertData * D = (LambertData *) nullptr; +LambertData * D = nullptr; D = new LambertData; memcpy(D, &data, sizeof(data)); -lc = D; D = (LambertData *) nullptr; +lc = D; D = nullptr; return; @@ -578,13 +578,13 @@ void GridInfo::set(const StereographicData & data) clear(); -StereographicData * D = (StereographicData *) nullptr; +StereographicData * D = nullptr; D = new StereographicData; memcpy(D, &data, sizeof(data)); -st = D; D = (StereographicData *) nullptr; +st = D; D = nullptr; return; @@ -600,13 +600,13 @@ void GridInfo::set(const LatLonData & data) clear(); -LatLonData * D = (LatLonData *) nullptr; +LatLonData * D = nullptr; D = new LatLonData; memcpy(D, &data, sizeof(data)); -ll = D; D = (LatLonData *) nullptr; +ll = D; D = nullptr; return; @@ -622,13 +622,13 @@ void GridInfo::set(const RotatedLatLonData & data) clear(); -RotatedLatLonData * D = (RotatedLatLonData *) nullptr; +RotatedLatLonData * D = nullptr; D = new RotatedLatLonData; memcpy(D, &data, sizeof(data)); -rll = D; D = (RotatedLatLonData *) nullptr; +rll = D; D = nullptr; return; @@ -644,13 +644,13 @@ void GridInfo::set(const MercatorData & data) clear(); -MercatorData * D = (MercatorData *) nullptr; +MercatorData * D = nullptr; D = new MercatorData; memcpy(D, &data, sizeof(data)); -m = D; D = (MercatorData *) nullptr; +m = D; D = nullptr; return; @@ -666,13 +666,13 @@ void GridInfo::set(const GaussianData & data) clear(); -GaussianData * D = (GaussianData *) nullptr; +GaussianData * D = nullptr; D = new GaussianData; memcpy(D, &data, sizeof(data)); -g = D; D = (GaussianData *) nullptr; +g = D; D = nullptr; return; @@ -688,13 +688,13 @@ void GridInfo::set(const GoesImagerData & data) clear(); -GoesImagerData * D = (GoesImagerData *) nullptr; +GoesImagerData * D = nullptr; D = new GoesImagerData; memcpy(D, &data, sizeof(data)); -gi = D; D = (GoesImagerData *) nullptr; +gi = D; D = nullptr; return; @@ -710,13 +710,13 @@ void GridInfo::set(const LaeaData & data) clear(); -LaeaData * D = (LaeaData *) nullptr; +LaeaData * D = nullptr; D = new LaeaData; memcpy(D, &data, sizeof(data)); -la = D; D = (LaeaData *) nullptr; +la = D; D = nullptr; return; @@ -732,7 +732,7 @@ void GridInfo::set(const SemiLatLonData & data) clear(); -SemiLatLonData * D = (SemiLatLonData *) nullptr; +SemiLatLonData * D = nullptr; D = new SemiLatLonData; @@ -743,7 +743,7 @@ D = new SemiLatLonData; *D = data; -sl = D; D = (SemiLatLonData *) nullptr; +sl = D; D = nullptr; return; @@ -760,7 +760,9 @@ void GridInfo::set(const UnstructuredData & data) clear(); -UnstructuredData *D = new UnstructuredData; +UnstructuredData * D = nullptr; + +D = new UnstructuredData; D->n_edge = data.n_edge; D->n_node = data.n_node; @@ -772,7 +774,7 @@ else { D->set_points(data.n_face, data.points_XYZ); } us = D; -D = (UnstructuredData *)nullptr; +D = nullptr; } #endif @@ -1250,7 +1252,8 @@ if ( (nx_new < 2) || ( ny_new < 2) ) { } Grid g_new; -double lat_ll, lon_ll; +double lat_ll; +double lon_ll; GridInfo info_new = info(); @@ -1302,7 +1305,8 @@ if ( info_new.lc ) { } else if ( info_new.m ) { MercatorData m_new = *(info_new.m); - double lat_ur, lon_ur; + double lat_ur; + double lon_ur; xy_to_latlon(x_ll + nx_new - 1, y_ll + ny_new - 1, lat_ur, lon_ur); @@ -1346,8 +1350,10 @@ Grid Grid::subset_center(double lat_center, double lon_center, int nx_new, int n // subset_ll does sanity checking on nx_new and ny_new, so we don't have to do it here // -int ix_ll, iy_ll; -double dx_center, dy_center; +int ix_ll; +int iy_ll; +double dx_center; +double dy_center; // @@ -1732,7 +1738,7 @@ return status; //////////////////////////////////////////////////////////////////////// -int ll_func(double x_center, int N) +static int ll_func(double x_center, int N) { diff --git a/src/libcode/vx_grid/unstructured_grid.cc b/src/libcode/vx_grid/unstructured_grid.cc index 9958a803e..7a7ec26f9 100644 --- a/src/libcode/vx_grid/unstructured_grid.cc +++ b/src/libcode/vx_grid/unstructured_grid.cc @@ -49,7 +49,7 @@ void llh_to_ecef(double lat, double lon, double alt_m, double *x_km, double *y_km, double *z_km); void check_llh_to_ecef(double lat, double lon, double alt_m, double true_x_km, double true_y_km, double true_z_km, - string location); + const string &location); //////////////////////////////////////////////////////////////////////// @@ -161,42 +161,10 @@ void UnstructuredGrid::latlon_to_xy(double lat, double lon, double &x, double &y //////////////////////////////////////////////////////////////////////// -//void UnstructuredGrid::latlonalt_to_xy(double lat, double lon, double alt_m, double &x, double &y) const { -// -// double x_km; -// double y_km; -// double z_km; -// llh_to_ecef(lat, lon, alt_m, &x_km, &y_km, &z_km); -// points_XYZ_km[i] = {x_km, y_km, z_km}; -// PointXYZ _pointXYZ(x_km, y_km, z_km); -// -// IndexKDTree::ValueList neighbor = Data.closest_points(lat, lon, 1, alt_m); -// size_t index(neighbor[0].payload()); -// double distance_km(neighbor[0].distance()/1000.); -// bool in_distance = Data.is_in_distance(distance_km); -// -// x = in_distance ? index : -1.0; -// y = 0; -// -// if(mlog.verbosity_level() >= UGRID_DEBUG_LEVEL) mlog -// << Debug(UGRID_DEBUG_LEVEL) << "UnstructuredGrid::latlonalt_to_xy() " -// << "input(lat,lon,alt)=(" << lat << ", " << lon <<", " << alt -// << ") ==> km(" << x_km << ", " << y_km << ", " << z_km -// << ") ==> (" << x << ", " << y << ") == (" -// << Data.points_XYZ[index].x() << ", " << Data.points_XYZ[index].y() -// << ", " << Data.points_XYZ[index].z() << ") distance= " << distance_km << "km, " -// << _pointXYZ.distance(Data.points_XYZ[index]) -// << " km" << (in_distance ? " " : ", rejected") << "\n"; -//} - - -//////////////////////////////////////////////////////////////////////// - - void UnstructuredGrid::xy_to_latlon(double x, double y, double &lat, double &lon) const { - lat = Data.points_lonlat[x].y(); - lon = Data.points_lonlat[x].x(); + lat = (double) Data.points_lonlat[x].y(); + lon = (double) Data.points_lonlat[x].x(); if(mlog.verbosity_level() >= UGRID_DEBUG_LEVEL) mlog << Debug(UGRID_DEBUG_LEVEL) << "UnstructuredGrid::xy_to_latlon() " @@ -207,6 +175,7 @@ void UnstructuredGrid::xy_to_latlon(double x, double y, double &lat, double &lon //////////////////////////////////////////////////////////////////////// + double UnstructuredGrid::calc_area(int x, int y) const { double area = 0.; @@ -215,6 +184,7 @@ double UnstructuredGrid::calc_area(int x, int y) const { } + //////////////////////////////////////////////////////////////////////// @@ -403,7 +373,8 @@ void UnstructuredData::build_tree() { for (int i=0; iinsert(pointLL, n++); + kdtree->insert(pointLL, n); + n++; lat_checksum += (i+1) * points_lonlat[i].y(); lon_checksum += (i+1) * points_lonlat[i].x(); } @@ -417,7 +388,8 @@ void UnstructuredData::build_tree() { llh_to_ecef(points_XYZ[i].y(), points_XYZ[i].x(), points_XYZ[i].z(), &x_km, &y_km, &z_km); points_XYZ_km.push_back({x_km, y_km, z_km}); - kdtree->insert(points_XYZ_km[i], n++); + kdtree->insert(points_XYZ_km[i], n); + n++; lat_checksum += (i+1) * y_km; lon_checksum += (i+1) * x_km; alt_checksum += (i+1) * z_km; @@ -488,7 +460,7 @@ void UnstructuredData::copy_from(const UnstructuredData *us_data) { //////////////////////////////////////////////////////////////////////// bool UnstructuredData::has_PointLatLon() const { - return (points_lonlat.size() > 0); + return (!points_lonlat.empty()); } //////////////////////////////////////////////////////////////////////// @@ -504,7 +476,7 @@ bool UnstructuredData::is_in_distance(double distance_km) const { //////////////////////////////////////////////////////////////////////// -void UnstructuredData::set_points(int count, double *_lon, double *_lat) { +void UnstructuredData::set_points(int count, const double *_lon, const double *_lat) { clear_data(); @@ -526,13 +498,10 @@ void UnstructuredData::set_points(int count, double *_lon, double *_lat) { //////////////////////////////////////////////////////////////////////// -void UnstructuredData::set_points(int count, double *_lon, double *_lat, double *_alt) { +void UnstructuredData::set_points(int count, const double *_lon, const double *_lat, const double *_alt) { clear_data(); - double x_km; - double y_km; - double z_km; n_face = count; points_XYZ.reserve(count); for (int i=0; i= UGRID_DEBUG_LEVEL) { - // mlog << Debug(UGRID_DEBUG_LEVEL) << method_name - // << "first: (" << points_XYZ[0].x() << ", " << points_XYZ[0].y() << ", " - // << points_XYZ[0].z() << ") and last (" << points_XYZ[last_i].x() << ", " - // << points_XYZ[last_i].y() << ", " << points_XYZ[last_i].z() << ") from (" - // << points_XYZ[0].x() << ", " << points_XYZ[0].y() << ", " << points_XYZ[0].z() - // << ") and (" << points_XYZ[last_i].x() << ", " << points_XYZ[last_i].y() - // << ", " << points_XYZ[last_i].z() << ")\n"; - //} } } @@ -690,7 +650,7 @@ void UnstructuredData::test_kdtree() { // - search index=1152 (-27.9711, 107.326, 100) ==> (-1678.837, 5381.522, -2973.724) km // - search index=2303 ( 75.6264, 287.326, 100) ==> ( 473.024, -1516.283, 6156.59 ) km -void UnstructuredData::test_llh_to_ecef() { +void UnstructuredData::test_llh_to_ecef() const { check_llh_to_ecef( 34.0522, -118.40806, 0., -2516.715, -4653.003, 3551.245, " LA"); check_llh_to_ecef(-9.59874, 287.326, 100., 1873.072, -6004.143, -1056.531, " First Point"); check_llh_to_ecef(-27.9711, 107.326, 100., -1678.837, 5381.522, -2973.724, " Middle Point"); @@ -715,7 +675,7 @@ void llh_to_ecef(double lat, double lon, double alt_m, double *x_km, double *y_k //////////////////////////////////////////////////////////////////////// -void check_llh_to_ecef(double lat, double lon, double alt_m, double true_x_km, double true_y_km, double true_z_km, string location) { +void check_llh_to_ecef(double lat, double lon, double alt_m, double true_x_km, double true_y_km, double true_z_km, const string &location) { double x_km; double y_km; double z_km; @@ -726,3 +686,5 @@ void check_llh_to_ecef(double lat, double lon, double alt_m, double true_x_km, d << ") Diff: (" << (true_x_km - x_km) << ", " << (true_y_km - y_km) << ", " << (true_z_km - z_km) << ")\n"; } + +//////////////////////////////////////////////////////////////////////// diff --git a/src/libcode/vx_grid/unstructured_grid.h b/src/libcode/vx_grid/unstructured_grid.h index 786ac5362..b21e7d32c 100644 --- a/src/libcode/vx_grid/unstructured_grid.h +++ b/src/libcode/vx_grid/unstructured_grid.h @@ -45,7 +45,6 @@ class UnstructuredGrid : public GridRep { void clear(); void set_from_data(const UnstructuredData &); - //void set_max_distance_deg(double max_distance); void set_max_distance_km(double max_distance); // @@ -56,10 +55,6 @@ class UnstructuredGrid : public GridRep { virtual void xy_to_latlon(double x, double y, double & lat, double & lon) const; - //void latlonalt_to_xy(double lat, double lon, double alt_m, double & x, double & y) const; - - //void xy_to_latlonalt(double x, double y, double & lat, double & lon, double &alt) const; - virtual double calc_area(int x, int y) const; virtual int nx() const; diff --git a/src/libcode/vx_grid/unstructured_grid_defs.h b/src/libcode/vx_grid/unstructured_grid_defs.h index f5dab8d20..2ec67504a 100644 --- a/src/libcode/vx_grid/unstructured_grid_defs.h +++ b/src/libcode/vx_grid/unstructured_grid_defs.h @@ -51,9 +51,9 @@ struct UnstructuredData { void build_tree(); bool is_in_distance(double distance_km) const; - void set_points(int count, double *_lon, double *_lat); + void set_points(int count, const double *_lon, const double *_lat); void set_points(int count, const std::vector &); - void set_points(int count, double *_lon, double *_lat, double *_alt); + void set_points(int count, const double *_lon, const double *_lat, const double *_alt); void set_points(int count, const std::vector &); void copy_from(const UnstructuredData *); void copy_from(const UnstructuredData &); @@ -61,7 +61,7 @@ struct UnstructuredData { void clear_data(); bool has_PointLatLon() const; void test_kdtree(); - void test_llh_to_ecef(); + void test_llh_to_ecef() const; atlas::util::IndexKDTree::ValueList closest_points( const double &lat, const double &lon, const size_t &k, const double &alt_m=bad_data_double) const;