Skip to content

Commit

Permalink
refactor height filter to appease cppcheck & MSVC (GPSBabel#473)
Browse files Browse the repository at this point in the history
* refactor heightgrid to appease cppcheck.

With newer versions (somwhere after 1.82) of cppcheck analyzing
height.cc took an excessive amount of time.
By making these variables private static class members instead of
member function static variables we avoid the cppcheck issues.

* tweak height to work around MSVC 2017 error.

and hopefully MSVC 2015 error as well.

* don't include cstdint within class definition.

It can lead to compile failures.  This was done in heightgrid.h,
but the include guards in cstdint hid the error.
  • Loading branch information
tsteven4 authored Jan 26, 2020
1 parent e483407 commit 340fffd
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 223 deletions.
1 change: 0 additions & 1 deletion .codacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ exclude_paths:
- 'shapelib/**'
- 'zlib/**'
- coverity_model.cc
- height.cc # It takes newer cppcheck versions, somewhere > 1.82, an excessive amount of time to analyze heightgrid.h
- strptime.[ch]
- jeeps/gpsproj.cc
- jeeps/gpsproj.h
45 changes: 26 additions & 19 deletions height.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@

#include "defs.h"
#include "height.h"
#include <cmath>
#include <cstdlib>
#include <cmath> // for floor
#include <cstdint> // for int8_t
#include <cstdlib> // for strtod

#define MYNAME "height"

#if FILTERS_ENABLED

// Until c++17 we have to define odr-used constexpr static data members at namespace scope.
#if __cplusplus < 201703L
constexpr double HeightFilter::geoid_grid_deg;
constexpr double HeightFilter::geoid_scale;
constexpr int HeightFilter::geoid_row;
constexpr int HeightFilter::geoid_col;
constexpr int8_t HeightFilter::geoid_delta[geoid_row][geoid_col];
#endif

double HeightFilter::bilinear(double x1, double y1, double x2, double y2, double x, double y, double z11, double z12, double z21, double z22)
{
if (y1 == y2 && x1 == x2) {
Expand All @@ -51,9 +61,6 @@ double HeightFilter::bilinear(double x1, double y1, double x2, double y2, double
/* return geoid separation (MSL - WGS84) in meters, given a lat/lot in degrees */
double HeightFilter::wgs84_separation(double lat, double lon)
{
#include "heightgrid.h"


/* sanity checks to prevent segfault on bad data */
if ((lat > 90.0) || (lat < -90.0)) {
fatal(MYNAME ": Invalid latitude value (%f)\n", lat);
Expand All @@ -62,35 +69,35 @@ double HeightFilter::wgs84_separation(double lat, double lon)
fatal(MYNAME ": Invalid longitude value (%f)\n", lon);
}

int ilat = (int)floor((90.0+lat)/GEOID_GRID_DEG);
int ilon = (int)floor((180.0+lon)/GEOID_GRID_DEG);
auto ilat = static_cast<int>(floor((90.0+lat)/geoid_grid_deg));
auto ilon = static_cast<int>(floor((180.0+lon)/geoid_grid_deg));

int ilat1 = ilat;
int ilon1 = ilon;
int ilat2 = (ilat < GEOID_ROW-1)? ilat+1:ilat;
int ilon2 = (ilon < GEOID_COL-1)? ilon+1:ilon;
int ilat2 = (ilat < geoid_row-1)? ilat+1:ilat;
int ilon2 = (ilon < geoid_col-1)? ilon+1:ilon;

return bilinear(
ilon1*GEOID_GRID_DEG-180.0,ilat1*GEOID_GRID_DEG-90.0,
ilon2*GEOID_GRID_DEG-180.0,ilat2*GEOID_GRID_DEG-90.0,
ilon1*geoid_grid_deg-180.0, ilat1*geoid_grid_deg-90.0,
ilon2*geoid_grid_deg-180.0, ilat2*geoid_grid_deg-90.0,
lon, lat,
(double)geoid_delta[ilon1+ilat1*GEOID_COL]/GEOID_SCALE,
(double)geoid_delta[ilon2+ilat1*GEOID_COL]/GEOID_SCALE,
(double)geoid_delta[ilon1+ilat2*GEOID_COL]/GEOID_SCALE,
(double)geoid_delta[ilon2+ilat2*GEOID_COL]/GEOID_SCALE
static_cast<double>(geoid_delta[ilat1][ilon1])/geoid_scale,
static_cast<double>(geoid_delta[ilat1][ilon2])/geoid_scale,
static_cast<double>(geoid_delta[ilat2][ilon1])/geoid_scale,
static_cast<double>(geoid_delta[ilat2][ilon2])/geoid_scale
);
}

void HeightFilter::correct_height(const Waypoint* wpt)
{
Waypoint* waypointp = const_cast<Waypoint*>(wpt);
auto* waypointp = const_cast<Waypoint*>(wpt);

if (waypointp->altitude != unknown_alt) {
if (addopt) {
if (addopt != nullptr) {
waypointp->altitude += addf;
}

if (wgs84tomslopt) {
if (wgs84tomslopt != nullptr) {
waypointp->altitude -= wgs84_separation(waypointp->latitude, waypointp->longitude);
}
}
Expand All @@ -100,7 +107,7 @@ void HeightFilter::init()
{
char* unit;

if (addopt) {
if (addopt != nullptr) {
addf = strtod(addopt, &unit);

if (*unit == 'f' || *unit== 'F') {
Expand Down
8 changes: 6 additions & 2 deletions height.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
#ifndef HEIGHT_H_INCLUDED_
#define HEIGHT_H_INCLUDED_

#include <cstdint> // for int8_t in heightgrid.h

#include <QtCore/QVector> // for QVector

#include "defs.h" // for ARG_NOMINMAX, Waypoint (ptr only), arglist_t
#include "filter.h" // for Filter
#include "defs.h" // for arglist_t, ARG_NOMINMAX, ARGTYPE_BEGIN_REQ, ARGTYPE_BOOL, ARGTYPE_END_REQ, ARGTYPE_FLOAT, Waypoint
#include "filter.h" // for Filter

#if FILTERS_ENABLED

Expand All @@ -45,6 +47,8 @@ class HeightFilter:public Filter
char* addopt = nullptr;
char* wgs84tomslopt = nullptr;
double addf;
// include static constexpr data member definitions with intializers for grid as private members.
#include "heightgrid.h"

QVector<arglist_t> args = {
{
Expand Down
Loading

0 comments on commit 340fffd

Please sign in to comment.