osrm-backend/src/engine/douglas_peucker.cpp

148 lines
5.2 KiB
C++
Raw Normal View History

2016-01-02 11:13:44 -05:00
#include "engine/douglas_peucker.hpp"
#include "util/coordinate_calculation.hpp"
2016-01-28 10:28:44 -05:00
#include "util/coordinate.hpp"
2014-11-28 04:07:06 -05:00
#include <boost/assert.hpp>
2016-01-28 10:28:44 -05:00
#include <boost/range/irange.hpp>
2014-11-28 04:07:06 -05:00
#include <cmath>
#include <algorithm>
2015-02-09 11:38:40 -05:00
#include <iterator>
#include <stack>
#include <utility>
2014-11-28 04:07:06 -05:00
2016-01-05 10:51:13 -05:00
namespace osrm
{
namespace engine
{
2014-11-28 04:07:06 -05:00
namespace
{
2016-01-28 10:28:44 -05:00
// FIXME This algorithm is a very naive approximation that leads to
// problems like (almost) co-linear points not being simplified.
// Switch to real-point-segment distance of projected coordinates
2014-11-28 04:07:06 -05:00
struct CoordinatePairCalculator
{
2016-01-28 10:28:44 -05:00
CoordinatePairCalculator() = delete;
CoordinatePairCalculator(const util::Coordinate coordinate_a,
const util::Coordinate coordinate_b)
2014-11-28 04:07:06 -05:00
{
2016-03-16 16:54:29 -04:00
using namespace util::coordinate_calculation;
2014-11-28 04:07:06 -05:00
// initialize distance calculator with two fixed coordinates a, b
2016-03-16 16:54:29 -04:00
first_lon = static_cast<double>(toFloating(coordinate_a.lon)) * DEGREE_TO_RAD;
first_lat = static_cast<double>(toFloating(coordinate_a.lat)) * DEGREE_TO_RAD;
second_lon = static_cast<double>(toFloating(coordinate_b.lon)) * DEGREE_TO_RAD;
second_lat = static_cast<double>(toFloating(coordinate_b.lat)) * DEGREE_TO_RAD;
2014-11-28 04:07:06 -05:00
}
int operator()(const util::Coordinate other) const
2014-11-28 04:07:06 -05:00
{
2016-03-16 16:54:29 -04:00
using namespace util::coordinate_calculation;
2014-11-28 04:07:06 -05:00
// set third coordinate c
2016-03-16 16:54:29 -04:00
const float float_lon1 = static_cast<double>(toFloating(other.lon)) * DEGREE_TO_RAD;
const float float_lat1 = static_cast<double>(toFloating(other.lat)) * DEGREE_TO_RAD;
2014-11-28 04:07:06 -05:00
// compute distance (a,c)
const float x_value_1 = (first_lon - float_lon1) * cos((float_lat1 + first_lat) / 2.f);
const float y_value_1 = first_lat - float_lat1;
2016-03-16 16:54:29 -04:00
const float dist1 = std::hypot(x_value_1, y_value_1) * EARTH_RADIUS;
2014-11-28 04:07:06 -05:00
// compute distance (b,c)
const float x_value_2 = (second_lon - float_lon1) * cos((float_lat1 + second_lat) / 2.f);
const float y_value_2 = second_lat - float_lat1;
2016-03-16 16:54:29 -04:00
const float dist2 = std::hypot(x_value_2, y_value_2) * EARTH_RADIUS;
2014-11-28 04:07:06 -05:00
// return the minimum
return static_cast<int>(std::min(dist1, dist2));
}
float first_lat;
float first_lon;
float second_lat;
float second_lon;
};
}
std::vector<util::Coordinate> douglasPeucker(std::vector<util::Coordinate>::const_iterator begin,
std::vector<util::Coordinate>::const_iterator end,
const unsigned zoom_level)
2014-11-28 04:07:06 -05:00
{
2016-01-28 10:28:44 -05:00
BOOST_ASSERT_MSG(zoom_level < detail::DOUGLAS_PEUCKER_THRESHOLDS_SIZE,
"unsupported zoom level");
2014-11-28 04:07:06 -05:00
const auto size = std::distance(begin, end);
2014-11-28 04:07:06 -05:00
if (size < 2)
{
2016-01-28 10:28:44 -05:00
return {};
2014-11-28 04:07:06 -05:00
}
2016-01-28 10:28:44 -05:00
std::vector<bool> is_necessary(size, false);
BOOST_ASSERT(is_necessary.size() >= 2);
is_necessary.front() = true;
is_necessary.back() = true;
using GeometryRange = std::pair<std::size_t, std::size_t>;
2014-11-28 04:07:06 -05:00
2016-01-28 10:28:44 -05:00
std::stack<GeometryRange> recursion_stack;
recursion_stack.emplace(0UL, size - 1);
2014-11-28 04:07:06 -05:00
// mark locations as 'necessary' by divide-and-conquer
while (!recursion_stack.empty())
{
// pop next element
const GeometryRange pair = recursion_stack.top();
recursion_stack.pop();
// sanity checks
2016-01-28 10:28:44 -05:00
BOOST_ASSERT_MSG(is_necessary[pair.first], "left border must be necessary");
BOOST_ASSERT_MSG(is_necessary[pair.second], "right border must be necessary");
BOOST_ASSERT_MSG(pair.second < size, "right border outside of geometry");
BOOST_ASSERT_MSG(pair.first <= pair.second, "left border on the wrong side");
2014-11-28 04:07:06 -05:00
int max_int_distance = 0;
2016-01-28 10:28:44 -05:00
auto farthest_entry_index = pair.second;
const CoordinatePairCalculator dist_calc(begin[pair.first], begin[pair.second]);
2014-11-28 04:07:06 -05:00
// sweep over range to find the maximum
2016-01-28 10:28:44 -05:00
for (auto idx = pair.first + 1; idx != pair.second; ++idx)
2014-11-28 04:07:06 -05:00
{
2016-01-28 10:28:44 -05:00
const int distance = dist_calc(begin[idx]);
2014-11-28 04:07:06 -05:00
// found new feasible maximum?
if (distance > max_int_distance &&
distance > detail::DOUGLAS_PEUCKER_THRESHOLDS[zoom_level])
2014-11-28 04:07:06 -05:00
{
2016-01-28 10:28:44 -05:00
farthest_entry_index = idx;
2014-11-28 04:07:06 -05:00
max_int_distance = distance;
}
}
// check if maximum violates a zoom level dependent threshold
if (max_int_distance > detail::DOUGLAS_PEUCKER_THRESHOLDS[zoom_level])
2014-11-28 04:07:06 -05:00
{
// mark idx as necessary
2016-01-28 10:28:44 -05:00
is_necessary[farthest_entry_index] = true;
if (pair.first < farthest_entry_index)
2014-11-28 04:07:06 -05:00
{
2016-01-28 10:28:44 -05:00
recursion_stack.emplace(pair.first, farthest_entry_index);
2014-11-28 04:07:06 -05:00
}
2016-01-28 10:28:44 -05:00
if (farthest_entry_index < pair.second)
2014-11-28 04:07:06 -05:00
{
2016-01-28 10:28:44 -05:00
recursion_stack.emplace(farthest_entry_index, pair.second);
2014-11-28 04:07:06 -05:00
}
}
}
2016-01-28 10:28:44 -05:00
auto simplified_size = std::count(is_necessary.begin(), is_necessary.end(), true);
std::vector<util::Coordinate> simplified_geometry;
2016-01-28 10:28:44 -05:00
simplified_geometry.reserve(simplified_size);
for (auto idx : boost::irange<std::size_t>(0UL, size))
{
if (is_necessary[idx])
{
simplified_geometry.push_back(begin[idx]);
}
}
return simplified_geometry;
2014-11-28 04:07:06 -05:00
}
} // ns engine
} // ns osrm