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/datafacade/datafacade_base.hpp"
|
|
|
|
#include "engine/api/base_parameters.hpp"
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/phantom_node.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/json_container.hpp"
|
|
|
|
#include "util/integer_range.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>
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace engine
|
|
|
|
{
|
|
|
|
namespace plugins
|
|
|
|
{
|
|
|
|
|
2014-05-02 12:06:31 -04:00
|
|
|
class BasePlugin
|
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
protected:
|
|
|
|
datafacade::BaseDataFacade &facade;
|
|
|
|
BasePlugin(datafacade::BaseDataFacade &facade_) : facade(facade_) {}
|
|
|
|
|
2016-02-23 15:23:13 -05:00
|
|
|
bool CheckAllCoordinates(const std::vector<util::Coordinate> &coordinates)
|
2015-12-17 10:45:15 -05:00
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
return !std::any_of(std::begin(coordinates), std::end(coordinates),
|
2016-02-23 15:23:13 -05:00
|
|
|
[](const util::Coordinate coordinate)
|
2016-02-20 22:27:26 -05:00
|
|
|
{
|
|
|
|
return !coordinate.IsValid();
|
|
|
|
});
|
2016-01-28 10:28:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
Status Error(const std::string &code,
|
|
|
|
const std::string &message,
|
|
|
|
util::json::Object &json_result) const
|
2014-10-22 13:02:19 -04:00
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
json_result.values["code"] = code;
|
|
|
|
json_result.values["message"] = message;
|
|
|
|
return Status::Error;
|
2014-10-22 13:02:19 -04:00
|
|
|
}
|
2015-12-01 15:05:36 -05:00
|
|
|
|
|
|
|
// Decides whether to use the phantom node from a big or small component if both are found.
|
|
|
|
// Returns true if all phantom nodes are in the same component after snapping.
|
2016-01-28 10:28:44 -05:00
|
|
|
std::vector<PhantomNode>
|
|
|
|
SnapPhantomNodes(const std::vector<PhantomNodePair> &phantom_node_pair_list) const
|
2015-12-01 15:05:36 -05:00
|
|
|
{
|
2015-12-16 15:54:07 -05:00
|
|
|
const auto check_component_id_is_tiny =
|
|
|
|
[](const std::pair<PhantomNode, PhantomNode> &phantom_pair)
|
2015-12-01 15:05:36 -05:00
|
|
|
{
|
|
|
|
return phantom_pair.first.component.is_tiny;
|
|
|
|
};
|
|
|
|
|
|
|
|
// are all phantoms from a tiny cc?
|
2015-12-16 15:54:07 -05:00
|
|
|
const auto check_all_in_same_component =
|
|
|
|
[](const std::vector<std::pair<PhantomNode, PhantomNode>> &nodes)
|
2015-12-01 15:05:36 -05:00
|
|
|
{
|
|
|
|
const auto component_id = nodes.front().first.component.id;
|
|
|
|
|
|
|
|
return std::all_of(std::begin(nodes), std::end(nodes),
|
|
|
|
[component_id](const PhantomNodePair &phantom_pair)
|
|
|
|
{
|
|
|
|
return component_id == phantom_pair.first.component.id;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-12-16 15:54:07 -05:00
|
|
|
const auto fallback_to_big_component =
|
|
|
|
[](const std::pair<PhantomNode, PhantomNode> &phantom_pair)
|
2015-12-01 15:05:36 -05:00
|
|
|
{
|
2016-01-05 06:41:16 -05:00
|
|
|
if (phantom_pair.first.component.is_tiny && phantom_pair.second.IsValid() &&
|
2015-12-16 15:54:07 -05:00
|
|
|
!phantom_pair.second.component.is_tiny)
|
2015-12-01 15:05:36 -05:00
|
|
|
{
|
2015-12-16 15:54:07 -05:00
|
|
|
return phantom_pair.second;
|
2015-12-01 15:05:36 -05:00
|
|
|
}
|
2015-12-16 15:54:07 -05:00
|
|
|
return phantom_pair.first;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto use_closed_phantom = [](const std::pair<PhantomNode, PhantomNode> &phantom_pair)
|
|
|
|
{
|
|
|
|
return phantom_pair.first;
|
2015-12-01 15:05:36 -05:00
|
|
|
};
|
|
|
|
|
2015-12-16 14:25:29 -05:00
|
|
|
const bool every_phantom_is_in_tiny_cc =
|
|
|
|
std::all_of(std::begin(phantom_node_pair_list), std::end(phantom_node_pair_list),
|
|
|
|
check_component_id_is_tiny);
|
2015-12-01 15:05:36 -05:00
|
|
|
auto all_in_same_component = check_all_in_same_component(phantom_node_pair_list);
|
|
|
|
|
2015-12-16 15:54:07 -05:00
|
|
|
std::vector<PhantomNode> snapped_phantoms;
|
|
|
|
snapped_phantoms.reserve(phantom_node_pair_list.size());
|
2015-12-01 15:05:36 -05:00
|
|
|
|
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
|
|
|
|
if (every_phantom_is_in_tiny_cc && all_in_same_component)
|
|
|
|
{
|
|
|
|
std::transform(phantom_node_pair_list.begin(), phantom_node_pair_list.end(),
|
|
|
|
std::back_inserter(snapped_phantoms), use_closed_phantom);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::transform(phantom_node_pair_list.begin(), phantom_node_pair_list.end(),
|
|
|
|
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>>
|
|
|
|
GetPhantomNodesInRange(const api::BaseParameters ¶meters,
|
|
|
|
const std::vector<double> radiuses) const
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (use_hints && parameters.hints[i] &&
|
|
|
|
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
|
|
|
|
{
|
|
|
|
phantom_nodes[i].push_back(PhantomNodeWithDistance{
|
|
|
|
parameters.hints[i]->phantom,
|
|
|
|
util::coordinate_calculation::haversineDistance(
|
|
|
|
parameters.coordinates[i], parameters.hints[i]->phantom.location),
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (use_bearings && parameters.bearings[i])
|
|
|
|
{
|
|
|
|
phantom_nodes[i] = facade.NearestPhantomNodesInRange(
|
|
|
|
parameters.coordinates[i], radiuses[i], parameters.bearings[i]->bearing,
|
|
|
|
parameters.bearings[i]->range);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
phantom_nodes[i] =
|
|
|
|
facade.NearestPhantomNodesInRange(parameters.coordinates[i], radiuses[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return phantom_nodes;
|
|
|
|
}
|
|
|
|
|
2016-02-23 15:23:13 -05:00
|
|
|
std::vector<std::vector<PhantomNodeWithDistance>>
|
|
|
|
GetPhantomNodes(const api::BaseParameters ¶meters, unsigned number_of_results)
|
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();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (use_hints && parameters.hints[i] &&
|
|
|
|
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
|
|
|
|
{
|
|
|
|
phantom_nodes[i].push_back(PhantomNodeWithDistance{
|
|
|
|
parameters.hints[i]->phantom,
|
|
|
|
util::coordinate_calculation::haversineDistance(
|
|
|
|
parameters.coordinates[i], parameters.hints[i]->phantom.location),
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_bearings && parameters.bearings[i])
|
|
|
|
{
|
|
|
|
if (use_radiuses && parameters.radiuses[i])
|
|
|
|
{
|
2016-02-23 15:23:13 -05:00
|
|
|
phantom_nodes[i] = facade.NearestPhantomNodes(
|
|
|
|
parameters.coordinates[i], number_of_results, *parameters.radiuses[i],
|
|
|
|
parameters.bearings[i]->bearing, parameters.bearings[i]->range);
|
2016-02-22 18:44:35 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-23 15:23:13 -05:00
|
|
|
phantom_nodes[i] = facade.NearestPhantomNodes(
|
|
|
|
parameters.coordinates[i], number_of_results,
|
|
|
|
parameters.bearings[i]->bearing, parameters.bearings[i]->range);
|
2016-02-22 18:44:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (use_radiuses && parameters.radiuses[i])
|
|
|
|
{
|
2016-02-23 15:23:13 -05:00
|
|
|
phantom_nodes[i] = facade.NearestPhantomNodes(
|
|
|
|
parameters.coordinates[i], number_of_results, *parameters.radiuses[i]);
|
2016-02-22 18:44:35 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
phantom_nodes[i] =
|
2016-02-23 15:23:13 -05:00
|
|
|
facade.NearestPhantomNodes(parameters.coordinates[i], number_of_results);
|
2016-02-22 18:44:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
std::vector<PhantomNodePair> GetPhantomNodes(const api::BaseParameters ¶meters)
|
|
|
|
{
|
|
|
|
std::vector<PhantomNodePair> phantom_node_pairs(parameters.coordinates.size());
|
|
|
|
|
|
|
|
const bool use_hints = !parameters.hints.empty();
|
|
|
|
const bool use_bearings = !parameters.bearings.empty();
|
|
|
|
const bool use_radiuses = !parameters.radiuses.empty();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (use_hints && parameters.hints[i] &&
|
|
|
|
parameters.hints[i]->IsValid(parameters.coordinates[i], facade))
|
|
|
|
{
|
|
|
|
phantom_node_pairs[i].first = parameters.hints[i]->phantom;
|
|
|
|
// we don't set the second one - it will be marked as invalid
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_bearings && parameters.bearings[i])
|
|
|
|
{
|
|
|
|
if (use_radiuses && parameters.radiuses[i])
|
|
|
|
{
|
|
|
|
phantom_node_pairs[i] =
|
|
|
|
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
|
|
|
|
parameters.coordinates[i], *parameters.radiuses[i],
|
|
|
|
parameters.bearings[i]->bearing, parameters.bearings[i]->range);
|
|
|
|
}
|
|
|
|
else
|
2016-02-20 22:27:26 -05:00
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
phantom_node_pairs[i] =
|
|
|
|
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
|
|
|
|
parameters.coordinates[i], parameters.bearings[i]->bearing,
|
|
|
|
parameters.bearings[i]->range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (use_radiuses && parameters.radiuses[i])
|
|
|
|
{
|
|
|
|
phantom_node_pairs[i] =
|
|
|
|
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
|
|
|
|
parameters.coordinates[i], *parameters.radiuses[i]);
|
|
|
|
}
|
|
|
|
else
|
2016-02-20 22:27:26 -05:00
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
phantom_node_pairs[i] =
|
|
|
|
facade.NearestPhantomNodeWithAlternativeFromBigComponent(
|
|
|
|
parameters.coordinates[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 16:30:31 -05:00
|
|
|
// we didn't find a fitting node, return error
|
2016-01-28 10:28:44 -05:00
|
|
|
if (!phantom_node_pairs[i].first.IsValid(facade.GetNumberOfNodes()))
|
|
|
|
{
|
2016-03-03 09:36:03 -05:00
|
|
|
// TODO document why?
|
2016-02-25 22:33:47 -05:00
|
|
|
phantom_node_pairs.pop_back();
|
2016-01-28 10:28:44 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
BOOST_ASSERT(phantom_node_pairs[i].first.IsValid(facade.GetNumberOfNodes()));
|
|
|
|
BOOST_ASSERT(phantom_node_pairs[i].second.IsValid(facade.GetNumberOfNodes()));
|
|
|
|
}
|
|
|
|
return phantom_node_pairs;
|
|
|
|
}
|
2010-07-09 05:05:40 -04:00
|
|
|
};
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 10:02:02 -05:00
|
|
|
#endif /* BASE_PLUGIN_HPP */
|