2015-01-13 10:02:02 -05:00
|
|
|
#ifndef BASE_PLUGIN_HPP
|
|
|
|
#define BASE_PLUGIN_HPP
|
2010-07-09 05:05:40 -04:00
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
#include "engine/api/base_parameters.hpp"
|
2019-08-02 10:40:55 -04:00
|
|
|
#include "engine/api/base_result.hpp"
|
2019-08-05 06:46:13 -04:00
|
|
|
#include "engine/api/flatbuffers/fbresult_generated.h"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "engine/datafacade/datafacade_base.hpp"
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/phantom_node.hpp"
|
2017-07-28 11:55:38 -04:00
|
|
|
#include "engine/routing_algorithms.hpp"
|
2016-01-28 10:28:44 -05:00
|
|
|
#include "engine/status.hpp"
|
2015-12-01 15:05:36 -05:00
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
#include "util/coordinate.hpp"
|
2016-02-20 22:27:26 -05:00
|
|
|
#include "util/coordinate_calculation.hpp"
|
2016-01-28 10:28:44 -05:00
|
|
|
#include "util/integer_range.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "util/json_container.hpp"
|
2011-01-09 16:42:27 -05:00
|
|
|
|
2015-02-26 10:09:48 -05:00
|
|
|
#include <algorithm>
|
2016-04-12 09:00:08 -04:00
|
|
|
#include <iterator>
|
2013-06-26 19:48:02 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2017-05-18 11:05:11 -04:00
|
|
|
#include <util/log.hpp>
|
|
|
|
|
2022-12-11 04:10:26 -05:00
|
|
|
namespace osrm::engine::plugins
|
2016-01-05 10:51:13 -05:00
|
|
|
{
|
|
|
|
|
2014-05-02 12:06:31 -04:00
|
|
|
class BasePlugin
|
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
protected:
|
2023-03-24 11:49:33 -04:00
|
|
|
BasePlugin() = default;
|
|
|
|
|
|
|
|
BasePlugin(const boost::optional<double> default_radius_) : default_radius(default_radius_) {}
|
|
|
|
|
2016-10-05 19:05:03 -04:00
|
|
|
bool CheckAllCoordinates(const std::vector<util::Coordinate> &coordinates) const
|
2015-12-17 10:45:15 -05:00
|
|
|
{
|
2024-05-06 03:14:46 -04:00
|
|
|
return !std::any_of(std::begin(coordinates),
|
|
|
|
std::end(coordinates),
|
|
|
|
[](const util::Coordinate coordinate)
|
|
|
|
{ return !coordinate.IsValid(); });
|
2016-01-28 10:28:44 -05:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:24:33 -04:00
|
|
|
bool CheckAlgorithms(const api::BaseParameters ¶ms,
|
|
|
|
const RoutingAlgorithmsInterface &algorithms,
|
2019-08-02 10:40:55 -04:00
|
|
|
osrm::engine::api::ResultT &result) const
|
2017-07-28 11:55:38 -04:00
|
|
|
{
|
|
|
|
if (algorithms.IsValid())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-16 16:21:19 -04:00
|
|
|
if (!algorithms.HasExcludeFlags() && !params.exclude.empty())
|
2017-07-28 11:55:38 -04:00
|
|
|
{
|
2017-08-16 16:21:19 -04:00
|
|
|
Error("NotImplemented", "This algorithm does not support exclude flags.", result);
|
2017-07-28 11:55:38 -04:00
|
|
|
return false;
|
|
|
|
}
|
2017-08-16 16:21:19 -04:00
|
|
|
if (algorithms.HasExcludeFlags() && !params.exclude.empty())
|
2017-07-28 11:55:38 -04:00
|
|
|
{
|
2017-08-16 16:21:19 -04:00
|
|
|
Error("InvalidValue", "Exclude flag combination is not supported.", result);
|
2017-07-28 11:55:38 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-14 17:24:33 -04:00
|
|
|
BOOST_ASSERT_MSG(false,
|
|
|
|
"There are only two reasons why the algorithm interface can be invalid.");
|
2017-07-28 11:55:38 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-15 04:40:23 -04:00
|
|
|
struct ErrorRenderer
|
|
|
|
{
|
2019-08-05 06:46:13 -04:00
|
|
|
std::string code;
|
|
|
|
std::string message;
|
|
|
|
|
2019-08-15 04:40:23 -04:00
|
|
|
ErrorRenderer(std::string code, std::string message)
|
|
|
|
: code(std::move(code)), message(std::move(message)){};
|
2019-08-05 06:46:13 -04:00
|
|
|
|
2019-08-15 04:40:23 -04:00
|
|
|
void operator()(util::json::Object &json_result)
|
|
|
|
{
|
2019-08-05 06:46:13 -04:00
|
|
|
json_result.values["code"] = code;
|
|
|
|
json_result.values["message"] = message;
|
|
|
|
};
|
2019-08-15 04:40:23 -04:00
|
|
|
void operator()(flatbuffers::FlatBufferBuilder &fb_result)
|
|
|
|
{
|
2019-08-15 08:39:02 -04:00
|
|
|
auto error = api::fbresult::CreateErrorDirect(fb_result, code.c_str(), message.c_str());
|
|
|
|
api::fbresult::FBResultBuilder response(fb_result);
|
|
|
|
response.add_error(true);
|
|
|
|
response.add_code(error);
|
|
|
|
fb_result.Finish(response.Finish());
|
2019-08-05 06:46:13 -04:00
|
|
|
};
|
2019-08-15 04:40:23 -04:00
|
|
|
void operator()(std::string &str_result)
|
|
|
|
{
|
2019-08-05 06:46:13 -04:00
|
|
|
str_result = str(boost::format("code=%1% message=%2%") % code % message);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
Status Error(const std::string &code,
|
|
|
|
const std::string &message,
|
2019-08-02 10:40:55 -04:00
|
|
|
osrm::engine::api::ResultT &result) const
|
2014-10-22 13:02:19 -04:00
|
|
|
{
|
2019-08-05 06:46:13 -04:00
|
|
|
mapbox::util::apply_visitor(ErrorRenderer(code, message), result);
|
2016-01-28 10:28:44 -05:00
|
|
|
return Status::Error;
|
2014-10-22 13:02:19 -04:00
|
|
|
}
|
2015-12-01 15:05:36 -05:00
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
// Decides whether to use the phantom candidates from big or small components if both are found.
|
|
|
|
std::vector<PhantomNodeCandidates>
|
|
|
|
SnapPhantomNodes(std::vector<PhantomCandidateAlternatives> alternatives_list) const
|
2015-12-01 15:05:36 -05:00
|
|
|
{
|
|
|
|
// are all phantoms from a tiny cc?
|
2022-08-27 06:36:20 -04:00
|
|
|
const auto all_in_same_tiny_component =
|
2024-05-06 03:14:46 -04:00
|
|
|
[](const std::vector<PhantomCandidateAlternatives> &alts_list)
|
|
|
|
{
|
|
|
|
return std::any_of(
|
|
|
|
alts_list.front().first.begin(),
|
|
|
|
alts_list.front().first.end(),
|
|
|
|
// For each of the first possible phantoms, check if all other
|
|
|
|
// positions in the list have a phantom from the same small component.
|
|
|
|
[&](const PhantomNode &phantom)
|
|
|
|
{
|
|
|
|
if (!phantom.component.is_tiny)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto component_id = phantom.component.id;
|
|
|
|
return std::all_of(
|
|
|
|
std::next(alts_list.begin()),
|
|
|
|
std::end(alts_list),
|
|
|
|
[component_id](const PhantomCandidateAlternatives &alternatives)
|
|
|
|
{ return candidatesHaveComponent(alternatives.first, component_id); });
|
|
|
|
});
|
|
|
|
};
|
2015-12-16 15:54:07 -05:00
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
// Move the alternative into the final list
|
2024-05-06 03:14:46 -04:00
|
|
|
const auto fallback_to_big_component = [](PhantomCandidateAlternatives &alternatives)
|
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
auto no_big_alternative = alternatives.second.empty();
|
|
|
|
return no_big_alternative ? std::move(alternatives.first)
|
|
|
|
: std::move(alternatives.second);
|
|
|
|
};
|
2015-12-01 15:05:36 -05:00
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
// Move the alternative into the final list
|
2024-05-06 03:14:46 -04:00
|
|
|
const auto use_closed_phantom = [](PhantomCandidateAlternatives &alternatives)
|
|
|
|
{ return std::move(alternatives.first); };
|
2015-12-01 15:05:36 -05:00
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
const auto no_alternatives =
|
|
|
|
std::all_of(alternatives_list.begin(),
|
|
|
|
alternatives_list.end(),
|
2024-05-06 03:14:46 -04:00
|
|
|
[](const PhantomCandidateAlternatives &alternatives)
|
|
|
|
{ return alternatives.second.empty(); });
|
2015-12-01 15:05:36 -05:00
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
std::vector<PhantomNodeCandidates> snapped_phantoms;
|
|
|
|
snapped_phantoms.reserve(alternatives_list.size());
|
2015-12-16 15:54:07 -05:00
|
|
|
// The only case we don't snap to the big component if all phantoms are in the same small
|
|
|
|
// component
|
2022-08-27 06:36:20 -04:00
|
|
|
if (no_alternatives || all_in_same_tiny_component(alternatives_list))
|
2015-12-16 15:54:07 -05:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
std::transform(alternatives_list.begin(),
|
|
|
|
alternatives_list.end(),
|
2016-05-27 15:05:04 -04:00
|
|
|
std::back_inserter(snapped_phantoms),
|
|
|
|
use_closed_phantom);
|
2015-12-16 15:54:07 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
std::transform(alternatives_list.begin(),
|
|
|
|
alternatives_list.end(),
|
2016-05-27 15:05:04 -04:00
|
|
|
std::back_inserter(snapped_phantoms),
|
|
|
|
fallback_to_big_component);
|
2015-12-01 15:05:36 -05:00
|
|
|
}
|
2015-12-11 11:37:48 -05:00
|
|
|
|
2015-12-16 15:54:07 -05:00
|
|
|
return snapped_phantoms;
|
2015-12-01 15:05:36 -05:00
|
|
|
}
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-02-20 22:27:26 -05:00
|
|
|
// Falls back to default_radius for non-set radii
|
|
|
|
std::vector<std::vector<PhantomNodeWithDistance>>
|
2016-10-05 19:05:03 -04:00
|
|
|
GetPhantomNodesInRange(const datafacade::BaseDataFacade &facade,
|
|
|
|
const api::BaseParameters ¶meters,
|
2022-07-04 16:46:59 -04:00
|
|
|
const std::vector<double> &radiuses,
|
2018-12-13 19:10:32 -05:00
|
|
|
bool use_all_edges = false) const
|
2016-02-20 22:27:26 -05:00
|
|
|
{
|
|
|
|
std::vector<std::vector<PhantomNodeWithDistance>> phantom_nodes(
|
|
|
|
parameters.coordinates.size());
|
2016-02-23 16:50:26 -05:00
|
|
|
BOOST_ASSERT(radiuses.size() == parameters.coordinates.size());
|
2016-02-20 22:27:26 -05:00
|
|
|
|
|
|
|
const bool use_hints = !parameters.hints.empty();
|
|
|
|
const bool use_bearings = !parameters.bearings.empty();
|
2017-05-23 06:23:22 -04:00
|
|
|
const bool use_approaches = !parameters.approaches.empty();
|
2016-02-20 22:27:26 -05:00
|
|
|
|
2016-04-12 06:42:16 -04:00
|
|
|
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
|
2016-02-20 22:27:26 -05:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
if (use_hints && parameters.hints[i] && !parameters.hints[i]->segment_hints.empty() &&
|
2016-02-20 22:27:26 -05:00
|
|
|
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
|
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
for (const auto &seg_hint : parameters.hints[i]->segment_hints)
|
|
|
|
{
|
|
|
|
phantom_nodes[i].push_back(PhantomNodeWithDistance{
|
|
|
|
seg_hint.phantom,
|
|
|
|
util::coordinate_calculation::greatCircleDistance(
|
|
|
|
parameters.coordinates[i], seg_hint.phantom.location)});
|
|
|
|
}
|
2016-02-20 22:27:26 -05:00
|
|
|
continue;
|
|
|
|
}
|
2022-08-27 06:36:20 -04:00
|
|
|
|
|
|
|
phantom_nodes[i] = facade.NearestPhantomNodesInRange(
|
|
|
|
parameters.coordinates[i],
|
|
|
|
radiuses[i],
|
|
|
|
use_bearings ? parameters.bearings[i] : boost::none,
|
|
|
|
use_approaches && parameters.approaches[i] ? parameters.approaches[i].get()
|
|
|
|
: engine::Approach::UNRESTRICTED,
|
|
|
|
use_all_edges);
|
2016-02-20 22:27:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return phantom_nodes;
|
|
|
|
}
|
|
|
|
|
2016-02-23 15:23:13 -05:00
|
|
|
std::vector<std::vector<PhantomNodeWithDistance>>
|
2016-10-05 19:05:03 -04:00
|
|
|
GetPhantomNodes(const datafacade::BaseDataFacade &facade,
|
|
|
|
const api::BaseParameters ¶meters,
|
2022-08-27 06:36:20 -04:00
|
|
|
size_t number_of_results) const
|
2016-02-22 18:44:35 -05:00
|
|
|
{
|
2016-02-23 15:23:13 -05:00
|
|
|
std::vector<std::vector<PhantomNodeWithDistance>> phantom_nodes(
|
|
|
|
parameters.coordinates.size());
|
2016-02-22 18:44:35 -05:00
|
|
|
|
|
|
|
const bool use_hints = !parameters.hints.empty();
|
|
|
|
const bool use_bearings = !parameters.bearings.empty();
|
|
|
|
const bool use_radiuses = !parameters.radiuses.empty();
|
2017-05-23 06:23:22 -04:00
|
|
|
const bool use_approaches = !parameters.approaches.empty();
|
2016-02-22 18:44:35 -05:00
|
|
|
|
|
|
|
BOOST_ASSERT(parameters.IsValid());
|
2016-04-12 06:42:16 -04:00
|
|
|
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
|
2016-02-22 18:44:35 -05:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
if (use_hints && parameters.hints[i] && !parameters.hints[i]->segment_hints.empty() &&
|
2016-02-22 18:44:35 -05:00
|
|
|
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
|
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
for (const auto &seg_hint : parameters.hints[i]->segment_hints)
|
2016-02-22 18:44:35 -05:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
phantom_nodes[i].push_back(PhantomNodeWithDistance{
|
|
|
|
seg_hint.phantom,
|
|
|
|
util::coordinate_calculation::greatCircleDistance(
|
|
|
|
parameters.coordinates[i], seg_hint.phantom.location)});
|
2016-02-22 18:44:35 -05:00
|
|
|
}
|
2022-08-27 06:36:20 -04:00
|
|
|
continue;
|
2016-02-22 18:44:35 -05:00
|
|
|
}
|
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
phantom_nodes[i] = facade.NearestPhantomNodes(
|
|
|
|
parameters.coordinates[i],
|
|
|
|
number_of_results,
|
2023-03-24 11:49:33 -04:00
|
|
|
use_radiuses ? parameters.radiuses[i] : default_radius,
|
2022-08-27 06:36:20 -04:00
|
|
|
use_bearings ? parameters.bearings[i] : boost::none,
|
|
|
|
use_approaches && parameters.approaches[i] ? parameters.approaches[i].get()
|
|
|
|
: engine::Approach::UNRESTRICTED);
|
|
|
|
|
2016-03-01 16:30:31 -05:00
|
|
|
// we didn't find a fitting node, return error
|
2016-02-22 18:44:35 -05:00
|
|
|
if (phantom_nodes[i].empty())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return phantom_nodes;
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
std::vector<PhantomCandidateAlternatives>
|
|
|
|
GetPhantomNodes(const datafacade::BaseDataFacade &facade,
|
|
|
|
const api::BaseParameters ¶meters) const
|
2016-01-28 10:28:44 -05:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
std::vector<PhantomCandidateAlternatives> alternatives(parameters.coordinates.size());
|
2016-01-28 10:28:44 -05:00
|
|
|
|
|
|
|
const bool use_hints = !parameters.hints.empty();
|
|
|
|
const bool use_bearings = !parameters.bearings.empty();
|
|
|
|
const bool use_radiuses = !parameters.radiuses.empty();
|
2017-05-22 10:07:12 -04:00
|
|
|
const bool use_approaches = !parameters.approaches.empty();
|
2019-02-14 20:14:50 -05:00
|
|
|
const bool use_all_edges = parameters.snapping == api::BaseParameters::SnappingType::Any;
|
2016-01-28 10:28:44 -05:00
|
|
|
|
|
|
|
BOOST_ASSERT(parameters.IsValid());
|
2016-04-12 06:42:16 -04:00
|
|
|
for (const auto i : util::irange<std::size_t>(0UL, parameters.coordinates.size()))
|
2016-01-28 10:28:44 -05:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
if (use_hints && parameters.hints[i] && !parameters.hints[i]->segment_hints.empty() &&
|
2016-01-28 10:28:44 -05:00
|
|
|
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
|
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
std::transform(parameters.hints[i]->segment_hints.begin(),
|
|
|
|
parameters.hints[i]->segment_hints.end(),
|
|
|
|
std::back_inserter(alternatives[i].first),
|
|
|
|
[](const auto &seg_hint) { return seg_hint.phantom; });
|
2016-01-28 10:28:44 -05:00
|
|
|
// we don't set the second one - it will be marked as invalid
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
alternatives[i] = facade.NearestCandidatesWithAlternativeFromBigComponent(
|
|
|
|
parameters.coordinates[i],
|
2023-03-24 11:49:33 -04:00
|
|
|
use_radiuses ? parameters.radiuses[i] : default_radius,
|
2022-08-27 06:36:20 -04:00
|
|
|
use_bearings ? parameters.bearings[i] : boost::none,
|
|
|
|
use_approaches && parameters.approaches[i] ? parameters.approaches[i].get()
|
|
|
|
: engine::Approach::UNRESTRICTED,
|
|
|
|
use_all_edges);
|
2016-01-28 10:28:44 -05:00
|
|
|
|
2016-03-01 16:30:31 -05:00
|
|
|
// we didn't find a fitting node, return error
|
2022-08-27 06:36:20 -04:00
|
|
|
if (alternatives[i].first.empty())
|
2016-01-28 10:28:44 -05:00
|
|
|
{
|
2017-01-09 15:40:33 -05:00
|
|
|
// This ensures the list of phantom nodes only consists of valid nodes.
|
|
|
|
// We can use this on the call-site to detect an error.
|
2022-08-27 06:36:20 -04:00
|
|
|
alternatives.pop_back();
|
2016-01-28 10:28:44 -05:00
|
|
|
break;
|
|
|
|
}
|
2022-08-27 06:36:20 -04:00
|
|
|
|
|
|
|
BOOST_ASSERT(!alternatives[i].first.empty());
|
2016-01-28 10:28:44 -05:00
|
|
|
}
|
2022-08-27 06:36:20 -04:00
|
|
|
return alternatives;
|
2016-01-28 10:28:44 -05:00
|
|
|
}
|
2020-09-30 21:44:22 -04:00
|
|
|
|
2022-08-27 06:36:20 -04:00
|
|
|
std::string
|
|
|
|
MissingPhantomErrorMessage(const std::vector<PhantomCandidateAlternatives> &alternatives,
|
|
|
|
const std::vector<util::Coordinate> &coordinates) const
|
2020-09-30 21:44:22 -04:00
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
BOOST_ASSERT(alternatives.size() < coordinates.size());
|
|
|
|
auto mismatch =
|
|
|
|
std::mismatch(alternatives.begin(),
|
|
|
|
alternatives.end(),
|
|
|
|
coordinates.begin(),
|
|
|
|
coordinates.end(),
|
2024-05-06 03:14:46 -04:00
|
|
|
[](const auto &candidates_pair, const auto &coordinate)
|
|
|
|
{
|
2022-08-27 06:36:20 -04:00
|
|
|
return std::any_of(candidates_pair.first.begin(),
|
|
|
|
candidates_pair.first.end(),
|
2024-05-06 03:14:46 -04:00
|
|
|
[&](const auto &phantom)
|
|
|
|
{ return phantom.input_location == coordinate; });
|
2022-08-27 06:36:20 -04:00
|
|
|
});
|
|
|
|
std::size_t missing_index = std::distance(alternatives.begin(), mismatch.first);
|
2020-09-30 21:44:22 -04:00
|
|
|
return std::string("Could not find a matching segment for coordinate ") +
|
|
|
|
std::to_string(missing_index);
|
|
|
|
}
|
2023-03-24 11:49:33 -04:00
|
|
|
|
|
|
|
const boost::optional<double> default_radius;
|
2010-07-09 05:05:40 -04:00
|
|
|
};
|
2022-12-20 12:00:11 -05:00
|
|
|
} // namespace osrm::engine::plugins
|
2016-01-05 10:51:13 -05:00
|
|
|
|
2015-01-13 10:02:02 -05:00
|
|
|
#endif /* BASE_PLUGIN_HPP */
|