Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc sonar fixes #5397

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* This will properly maintain the copyright information. Maxar
* copyrights will be updated automatically.
*
* @copyright Copyright (C) 2015, 2016, 2017, 2018, 2021 Maxar (http://www.maxar.com/)
* @copyright Copyright (C) 2015, 2016, 2017, 2018, 2021, 2022 Maxar (http://www.maxar.com/)
*/
#include "Histogram.h"

Expand Down Expand Up @@ -59,52 +59,40 @@ double Histogram::diff(Histogram& other)
assert(_bins.size() == other._bins.size());
double diff = 0.0;
for (size_t i = 0; i < _bins.size(); i++)
{
diff += fabs(_bins[i] - other._bins[i]);
}

return diff / 2.0;
}

size_t Histogram::getBin(Radians theta) const
{
while (theta < 0.0)
{
theta += 2 * M_PI;
}
return (theta / (2 * M_PI)) * _bins.size();
return static_cast<size_t>((theta / (2 * M_PI)) * static_cast<double>(_bins.size()));
}

Radians Histogram::_getBinAngle(size_t i) const
{
return 2 * M_PI / _bins.size() * i + M_PI / _bins.size();
return 2 * M_PI / static_cast<double>(_bins.size()) * static_cast<int>(i) + M_PI / static_cast<double>(_bins.size());
}

Radians Histogram::getBinCenter(size_t bin) const
{
assert(bin < _bins.size());

Radians binSize = 2.0 * M_PI / (double)_bins.size();
return bin * binSize + binSize / 2.0;
return static_cast<double>(bin) * binSize + binSize / 2.0;
}

void Histogram::normalize()
{
double sum = 0.0;
for (size_t i = 0; i < _bins.size(); i++)
{
sum += _bins[i];
}
for (auto val : _bins)
sum += val;

if (sum <= 0.0)
{
sum = 1.0;
}

for (size_t i = 0; i < _bins.size(); i++)
{
_bins[i] /= sum;
}
}

void Histogram::smooth(Radians sigma)
Expand All @@ -129,12 +117,7 @@ QString Histogram::toString() const
{
QStringList l;
for (size_t i = 0; i < _bins.size(); ++i)
{
l <<
QString("%1°: %2")
.arg(QString::number(toDegrees(getBinCenter(i)), 'g', 6))
.arg(QString::number(_bins[i], 'g', 6));
}
l << QString("%1°: %2").arg(QString::number(toDegrees(getBinCenter(i)), 'g', 6), QString::number(_bins[i], 'g', 6));
return l.join(", ");
}

Expand All @@ -144,12 +127,7 @@ QString Histogram::printPositiveBins() const
for (size_t i = 0; i < _bins.size(); ++i)
{
if (_bins[i] > 0.0)
{
l <<
QString("%1°: %2")
.arg(QString::number(toDegrees(getBinCenter(i)), 'g', 6))
.arg(QString::number(_bins[i], 'g', 6));
}
l << QString("%1°: %2").arg(QString::number(toDegrees(getBinCenter(i)), 'g', 6), QString::number(_bins[i], 'g', 6));
}
return l.join(", ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* copyrights will be updated automatically.
*
* @copyright Copyright (C) 2005 VividSolutions (http://www.vividsolutions.com/)
* @copyright Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020, 2021 Maxar (http://www.maxar.com/)
* @copyright Copyright (C) 2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022 Maxar (http://www.maxar.com/)
*/

#include "LocationOfPoint.h"
Expand All @@ -33,8 +33,8 @@
#include <geos/geom/LineString.h>

// Hoot
#include <hoot/core/geometry/ElementToGeometryConverter.h>
#include <hoot/core/elements/OsmMap.h>
#include <hoot/core/geometry/ElementToGeometryConverter.h>

// Standard
#include <limits>
Expand All @@ -45,9 +45,9 @@ using namespace geos::geom;
namespace hoot
{

LocationOfPoint::LocationOfPoint(const ConstOsmMapPtr& map, ConstWayPtr way) :
_map(map),
_way(way)
LocationOfPoint::LocationOfPoint(const ConstOsmMapPtr& map, ConstWayPtr way)
: _map(map),
_way(way)
{
_length = -1;
}
Expand All @@ -62,18 +62,12 @@ double distance(const Node& n1, const Node& n2)
Coordinate LocationOfPoint::locate(double d)
{
if (_length == -1)
{
_length = ElementToGeometryConverter(_map).convertToLineString(_way)->getLength();
}

if (d <= 0)
{
return _map->getNode(_way->getNodeId(0))->toCoordinate();
}
else if (d >= _length)
{
return _map->getNode(_way->getNodeId(_way->getNodeCount() - 1))->toCoordinate();
}
return _map->getNode(_way->getNodeId(static_cast<int>(_way->getNodeCount() - 1)))->toCoordinate();

double running = 0.0;
double step = 0.0;
Expand All @@ -100,8 +94,7 @@ Coordinate LocationOfPoint::locate(double d)
return result;
}

WayLocation LocationOfPoint::locate(const ConstOsmMapPtr& map, ConstWayPtr way,
const Coordinate& inputPt)
WayLocation LocationOfPoint::locate(const ConstOsmMapPtr& map, ConstWayPtr way, const Coordinate& inputPt)
{
LocationOfPoint locater(map, way);
return locater.locate(inputPt);
Expand All @@ -119,12 +112,9 @@ WayLocation LocationOfPoint::locate(const Coordinate& inputPt) const

if (_length == -1)
{
std::shared_ptr<geos::geom::LineString> lineString =
ElementToGeometryConverter(_map).convertToLineString(_way);
std::shared_ptr<geos::geom::LineString> lineString = ElementToGeometryConverter(_map).convertToLineString(_way);
if (lineString)
{
_length = lineString->getLength();
}
else
{
LOG_TRACE("Invalid line string. Returning empty way location...");
Expand All @@ -134,43 +124,36 @@ WayLocation LocationOfPoint::locate(const Coordinate& inputPt) const
LOG_VART(_length);

if (_length <= 0.0)
{
return WayLocation(_map, _way, 0, 0);
}

if (_way->getNodeCount() >= 1)
{
// this little bit of fanciness makes the function ~30% faster.
Coordinate lastCoord = _map->getNode(_way->getNodeId(0))->toCoordinate();
for (size_t i = 0; i < _way->getNodeCount() - 1; i++)
{
ConstNodePtr node = _map->getNode(_way->getNodeId((long)(i + 1)));
ConstNodePtr node = _map->getNode(_way->getNodeId(static_cast<int>(i + 1)));
Coordinate nextCoord = node->toCoordinate();
seg.p0 = lastCoord;
seg.p1 = nextCoord;
lastCoord = nextCoord;
double segDistance = seg.distance(inputPt);
double segFrac = segmentFraction(seg, inputPt);

if (segDistance < minDistance)
{
minIndex = i;
minIndex = static_cast<int>(i);
minFrac = segFrac;
minDistance = segDistance;
}
}
}

return WayLocation(_map, _way, minIndex, minFrac);
}

WayLocation LocationOfPoint::locateAfter(const Coordinate& inputPt, const WayLocation& minLocation)
const
WayLocation LocationOfPoint::locateAfter(const Coordinate& inputPt, const WayLocation& minLocation) const
{
if (minLocation.isValid() == false)
{
return locate(inputPt);
}

assert(minLocation.getWay() == _way);

Expand All @@ -183,64 +166,48 @@ WayLocation LocationOfPoint::locateAfter(const Coordinate& inputPt, const WayLoc

for (size_t i = startIndex; i < _way->getNodeCount() - 1; i++)
{
seg.p0 = _map->getNode(_way->getNodeId(i))->toCoordinate();
seg.p1 = _map->getNode(_way->getNodeId(i + 1))->toCoordinate();
seg.p0 = _map->getNode(_way->getNodeId(static_cast<int>(i)))->toCoordinate();
seg.p1 = _map->getNode(_way->getNodeId(static_cast<int>(i + 1)))->toCoordinate();

if (i == startIndex)
{
seg.p0 = minLocation.getCoordinate();
}

double segDistance = seg.distance(inputPt);
double segFrac = segmentFraction(seg, inputPt);

if (segDistance < minDistance)
{
// if this is the first case (a partial line segment)
if (i == startIndex)
{
// recalculate the segFrac in terms of the whole line segment.
segFrac =
minLocation.getSegmentFraction() + (1 - minLocation.getSegmentFraction()) * segFrac;
}
nextClosestLocation = WayLocation(_map, _way, i, segFrac);
if (i == startIndex) // recalculate the segFrac in terms of the whole line segment.
segFrac = minLocation.getSegmentFraction() + (1 - minLocation.getSegmentFraction()) * segFrac;
nextClosestLocation = WayLocation(_map, _way, static_cast<int>(i), segFrac);
minDistance = segDistance;
}
}
// Return the minDistanceLocation found. This will not be null, since it was initialized to
// minLocation.
if (!(nextClosestLocation >= minLocation))
{
throw HootException("Computed location is before specified minimum location");
}

return nextClosestLocation;
}

bool LocationOfPoint::isGreater(int i, double segFrac, const WayLocation& loc) const
{
return WayLocation::compareLocationValues(i, segFrac,
loc.getSegmentIndex(), loc.getSegmentFraction()) > 0;
return WayLocation::compareLocationValues(i, segFrac, loc.getSegmentIndex(), loc.getSegmentFraction()) > 0;
}

double LocationOfPoint::segmentFraction(const LineSegment& seg, const Coordinate& inputPt)
{
double segFrac;
if (seg.p0.equals2D(seg.p1))
{
segFrac = 0.0;
}
else
{
segFrac = seg.projectionFactor(inputPt);
if (segFrac < 0.000001)
{
segFrac = 0.0;
}
else if (segFrac > 0.999999)
{
segFrac = 1.0;
}
}
return segFrac;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* copyrights will be updated automatically.
*
* @copyright Copyright (C) 2005 VividSolutions (http://www.vividsolutions.com/)
* @copyright Copyright (C) 2015, 2017, 2018, 2019, 2020, 2021 Maxar (http://www.maxar.com/)
* @copyright Copyright (C) 2015, 2017, 2018, 2019, 2020, 2021, 2022 Maxar (http://www.maxar.com/)
*/

#ifndef LOCATIONOFPOINT_H
Expand Down Expand Up @@ -52,7 +52,7 @@ class LocationOfPoint
LocationOfPoint(const ConstOsmMapPtr& map, ConstWayPtr way);

static WayLocation locate(const ConstOsmMapPtr& map, ConstWayPtr way,
const geos::geom::Coordinate& inputPt);
const geos::geom::Coordinate& inputPt);

/**
* @brief locate finds the nearest location along a {@link Way} to a given point.
Expand Down Expand Up @@ -90,8 +90,8 @@ class LocationOfPoint
*/
bool isGreater(int i, double segFrac, const WayLocation& loc) const;

static double segmentFraction(
const geos::geom::LineSegment& seg, const geos::geom::Coordinate& inputPt);
static double segmentFraction(const geos::geom::LineSegment& seg,
const geos::geom::Coordinate& inputPt);

private:

Expand Down
Loading