2016-02-15 21:20:22 -05:00
|
|
|
#ifndef ENGINE_API_TABLE_HPP
|
|
|
|
#define ENGINE_API_TABLE_HPP
|
|
|
|
|
|
|
|
#include "engine/api/base_api.hpp"
|
|
|
|
#include "engine/api/json_factory.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "engine/api/table_parameters.hpp"
|
2016-02-15 21:20:22 -05:00
|
|
|
|
|
|
|
#include "engine/datafacade/datafacade_base.hpp"
|
|
|
|
|
|
|
|
#include "engine/guidance/assemble_geometry.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "engine/guidance/assemble_leg.hpp"
|
2016-02-15 21:20:22 -05:00
|
|
|
#include "engine/guidance/assemble_overview.hpp"
|
2016-05-27 15:05:04 -04:00
|
|
|
#include "engine/guidance/assemble_route.hpp"
|
2016-02-15 21:20:22 -05:00
|
|
|
#include "engine/guidance/assemble_steps.hpp"
|
|
|
|
|
|
|
|
#include "engine/internal_route_result.hpp"
|
|
|
|
|
|
|
|
#include "util/integer_range.hpp"
|
|
|
|
|
2016-03-08 10:46:01 -05:00
|
|
|
#include <boost/range/algorithm/transform.hpp>
|
|
|
|
|
2016-04-12 09:00:08 -04:00
|
|
|
#include <iterator>
|
|
|
|
|
2016-02-15 21:20:22 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace engine
|
|
|
|
{
|
|
|
|
namespace api
|
|
|
|
{
|
|
|
|
|
2016-02-17 19:41:50 -05:00
|
|
|
class TableAPI final : public BaseAPI
|
2016-02-15 21:20:22 -05:00
|
|
|
{
|
|
|
|
public:
|
2018-12-11 12:21:57 -05:00
|
|
|
struct TableCellRef
|
|
|
|
{
|
|
|
|
TableCellRef(const std::size_t &row, const std::size_t &column) : row{row}, column{column}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
std::size_t row;
|
|
|
|
std::size_t column;
|
|
|
|
};
|
|
|
|
|
2016-02-17 19:41:50 -05:00
|
|
|
TableAPI(const datafacade::BaseDataFacade &facade_, const TableParameters ¶meters_)
|
|
|
|
: BaseAPI(facade_, parameters_), parameters(parameters_)
|
2016-02-15 21:20:22 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-05 10:40:26 -04:00
|
|
|
virtual void
|
|
|
|
MakeResponse(const std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>> &tables,
|
|
|
|
const std::vector<PhantomNode> &phantoms,
|
|
|
|
const std::vector<TableCellRef> &fallback_speed_cells,
|
|
|
|
osrm::engine::api::ResultT &response) const
|
|
|
|
{
|
|
|
|
if(response.is<flatbuffers::FlatBufferBuilder>()) {
|
|
|
|
auto& fb_result = response.get<flatbuffers::FlatBufferBuilder>();
|
|
|
|
MakeResponse(tables, phantoms, fallback_speed_cells, fb_result);
|
|
|
|
} else {
|
|
|
|
auto& json_result = response.get<util::json::Object>();
|
|
|
|
MakeResponse(tables, phantoms, fallback_speed_cells, json_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
MakeResponse(const std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>> &tables,
|
|
|
|
const std::vector<PhantomNode> &phantoms,
|
|
|
|
const std::vector<TableCellRef> &fallback_speed_cells,
|
|
|
|
flatbuffers::FlatBufferBuilder &fb_result) const {
|
|
|
|
auto number_of_sources = parameters.sources.size();
|
|
|
|
auto number_of_destinations = parameters.destinations.size();
|
|
|
|
|
|
|
|
fbresult::FBResultBuilder response(fb_result);
|
|
|
|
response.add_code(fb_result.CreateString("Ok"));
|
|
|
|
response.add_response_type(osrm::engine::api::fbresult::ServiceResponse::ServiceResponse_table);
|
|
|
|
|
|
|
|
fbresult::TableBuilder table(fb_result);
|
|
|
|
|
|
|
|
// symmetric case
|
|
|
|
if (parameters.sources.empty())
|
|
|
|
{
|
|
|
|
table.add_sources(MakeWaypoints(fb_result, phantoms));
|
|
|
|
number_of_sources = phantoms.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table.add_sources(MakeWaypoints(fb_result, phantoms, parameters.sources));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.destinations.empty())
|
|
|
|
{
|
|
|
|
table.add_destinations(MakeWaypoints(fb_result, phantoms));
|
|
|
|
number_of_destinations = phantoms.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
table.add_destinations(MakeWaypoints(fb_result, phantoms, parameters.destinations));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.annotations & TableParameters::AnnotationsType::Duration)
|
|
|
|
{
|
|
|
|
table.add_durations(MakeDurationTable(fb_result, tables.first, number_of_sources, number_of_destinations));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.annotations & TableParameters::AnnotationsType::Distance)
|
|
|
|
{
|
|
|
|
table.add_distances(MakeDistanceTable(fb_result, tables.second, number_of_sources, number_of_destinations));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.fallback_speed != INVALID_FALLBACK_SPEED && parameters.fallback_speed > 0)
|
|
|
|
{
|
|
|
|
table.add_fallback_speed_cells(MakeEstimatesTable(fb_result, fallback_speed_cells));
|
|
|
|
}
|
|
|
|
|
|
|
|
fb_result.Finish(response.Finish());
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:18:55 -04:00
|
|
|
virtual void
|
|
|
|
MakeResponse(const std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>> &tables,
|
|
|
|
const std::vector<PhantomNode> &phantoms,
|
2018-12-11 12:21:57 -05:00
|
|
|
const std::vector<TableCellRef> &fallback_speed_cells,
|
2018-04-20 18:18:55 -04:00
|
|
|
util::json::Object &response) const
|
2016-02-15 21:20:22 -05:00
|
|
|
{
|
2016-03-03 18:23:38 -05:00
|
|
|
auto number_of_sources = parameters.sources.size();
|
2016-03-03 08:26:13 -05:00
|
|
|
auto number_of_destinations = parameters.destinations.size();
|
2016-03-03 18:23:38 -05:00
|
|
|
|
2016-02-15 21:20:22 -05:00
|
|
|
// symmetric case
|
|
|
|
if (parameters.sources.empty())
|
|
|
|
{
|
|
|
|
response.values["sources"] = MakeWaypoints(phantoms);
|
2016-03-03 18:23:38 -05:00
|
|
|
number_of_sources = phantoms.size();
|
2016-02-15 21:20:22 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
response.values["sources"] = MakeWaypoints(phantoms, parameters.sources);
|
2016-03-03 18:23:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.destinations.empty())
|
|
|
|
{
|
|
|
|
response.values["destinations"] = MakeWaypoints(phantoms);
|
|
|
|
number_of_destinations = phantoms.size();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-15 21:20:22 -05:00
|
|
|
response.values["destinations"] = MakeWaypoints(phantoms, parameters.destinations);
|
|
|
|
}
|
2016-03-03 18:23:38 -05:00
|
|
|
|
2018-04-20 18:18:55 -04:00
|
|
|
if (parameters.annotations & TableParameters::AnnotationsType::Duration)
|
|
|
|
{
|
|
|
|
response.values["durations"] =
|
|
|
|
MakeDurationTable(tables.first, number_of_sources, number_of_destinations);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parameters.annotations & TableParameters::AnnotationsType::Distance)
|
|
|
|
{
|
|
|
|
response.values["distances"] =
|
|
|
|
MakeDistanceTable(tables.second, number_of_sources, number_of_destinations);
|
|
|
|
}
|
|
|
|
|
2018-12-11 12:21:57 -05:00
|
|
|
if (parameters.fallback_speed != INVALID_FALLBACK_SPEED && parameters.fallback_speed > 0)
|
|
|
|
{
|
|
|
|
response.values["fallback_speed_cells"] = MakeEstimatesTable(fallback_speed_cells);
|
|
|
|
}
|
|
|
|
|
2016-04-04 08:05:38 -04:00
|
|
|
response.values["code"] = "Ok";
|
2016-02-15 21:20:22 -05:00
|
|
|
}
|
|
|
|
|
2016-12-15 09:28:54 -05:00
|
|
|
protected:
|
2019-08-05 10:40:26 -04:00
|
|
|
virtual flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<fbresult::Waypoint>>>
|
|
|
|
MakeWaypoints(flatbuffers::FlatBufferBuilder& builder, const std::vector<PhantomNode> &phantoms) const
|
|
|
|
{
|
|
|
|
std::vector<flatbuffers::Offset<fbresult::Waypoint>> waypoints;
|
|
|
|
waypoints.reserve(phantoms.size());
|
|
|
|
BOOST_ASSERT(phantoms.size() == parameters.coordinates.size());
|
|
|
|
|
|
|
|
boost::range::transform(
|
|
|
|
phantoms,
|
|
|
|
std::back_inserter(waypoints),
|
|
|
|
[this, &builder](const PhantomNode &phantom) { return BaseAPI::MakeWaypoint(builder, phantom); });
|
|
|
|
return builder.CreateVector(waypoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<fbresult::Waypoint>>>
|
|
|
|
MakeWaypoints(flatbuffers::FlatBufferBuilder& builder,
|
|
|
|
const std::vector<PhantomNode> &phantoms,
|
|
|
|
const std::vector<std::size_t> &indices) const
|
|
|
|
{
|
|
|
|
std::vector<flatbuffers::Offset<fbresult::Waypoint>> waypoints;
|
|
|
|
waypoints.reserve(indices.size());
|
|
|
|
boost::range::transform(indices,
|
|
|
|
std::back_inserter(waypoints),
|
|
|
|
[this, &builder, phantoms](const std::size_t idx) {
|
|
|
|
BOOST_ASSERT(idx < phantoms.size());
|
|
|
|
return BaseAPI::MakeWaypoint(builder, phantoms[idx]);
|
|
|
|
});
|
|
|
|
return builder.CreateVector(waypoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<fbresult::VectorDouble>>>
|
|
|
|
MakeDurationTable(flatbuffers::FlatBufferBuilder& builder,
|
|
|
|
const std::vector<EdgeWeight> &values,
|
|
|
|
std::size_t number_of_rows,
|
|
|
|
std::size_t number_of_columns) const
|
|
|
|
{
|
|
|
|
std::vector<flatbuffers::Offset<fbresult::VectorDouble>> fb_table;
|
|
|
|
for (const auto row : util::irange<std::size_t>(0UL, number_of_rows))
|
|
|
|
{
|
|
|
|
std::vector<double> fb_row;
|
|
|
|
auto row_begin_iterator = values.begin() + (row * number_of_columns);
|
|
|
|
auto row_end_iterator = values.begin() + ((row + 1) * number_of_columns);
|
|
|
|
fb_row.resize(number_of_columns);
|
|
|
|
std::transform(row_begin_iterator,
|
|
|
|
row_end_iterator,
|
|
|
|
fb_row.begin(),
|
|
|
|
[](const EdgeWeight duration) -> double {
|
|
|
|
if (duration == MAXIMAL_EDGE_DURATION)
|
|
|
|
{
|
|
|
|
return MAXIMAL_EDGE_DURATION;
|
|
|
|
}
|
|
|
|
// division by 10 because the duration is in deciseconds (10s)
|
|
|
|
return duration / 10.;
|
|
|
|
});
|
|
|
|
fb_table.push_back(fbresult::CreateVectorDoubleDirect(builder, &fb_row));
|
|
|
|
}
|
|
|
|
return builder.CreateVector(fb_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<fbresult::VectorDouble>>>
|
|
|
|
MakeDistanceTable(flatbuffers::FlatBufferBuilder& builder,
|
|
|
|
const std::vector<EdgeDistance> &values,
|
|
|
|
std::size_t number_of_rows,
|
|
|
|
std::size_t number_of_columns) const
|
|
|
|
{
|
|
|
|
std::vector<flatbuffers::Offset<fbresult::VectorDouble>> fb_table;
|
|
|
|
for (const auto row : util::irange<std::size_t>(0UL, number_of_rows))
|
|
|
|
{
|
|
|
|
std::vector<double> fb_row;
|
|
|
|
auto row_begin_iterator = values.begin() + (row * number_of_columns);
|
|
|
|
auto row_end_iterator = values.begin() + ((row + 1) * number_of_columns);
|
|
|
|
fb_row.resize(number_of_columns);
|
|
|
|
std::transform(row_begin_iterator,
|
|
|
|
row_end_iterator,
|
|
|
|
fb_row.begin(),
|
|
|
|
[](const EdgeDistance distance) -> double {
|
|
|
|
if (distance == INVALID_EDGE_DISTANCE) {
|
|
|
|
return INVALID_EDGE_DISTANCE;
|
|
|
|
}
|
|
|
|
// round to single decimal place
|
|
|
|
return std::round(distance * 10) / 10.;
|
|
|
|
});
|
|
|
|
fb_table.push_back(fbresult::CreateVectorDoubleDirect(builder, &fb_row));
|
|
|
|
}
|
|
|
|
return builder.CreateVector(fb_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<fbresult::VectorDouble>>>
|
|
|
|
MakeEstimatesTable(flatbuffers::FlatBufferBuilder& builder, const std::vector<TableCellRef> &fallback_speed_cells) const
|
|
|
|
{
|
|
|
|
std::vector<flatbuffers::Offset<fbresult::VectorDouble>> fb_table;
|
|
|
|
fb_table.reserve(fallback_speed_cells.size());
|
|
|
|
std::for_each(
|
|
|
|
fallback_speed_cells.begin(), fallback_speed_cells.end(), [&](const auto &cell) {
|
|
|
|
std::vector<double> fb_row;
|
|
|
|
fb_row.push_back(cell.row);
|
|
|
|
fb_row.push_back(cell.column);
|
|
|
|
fb_table.push_back(fbresult::CreateVectorDoubleDirect(builder, &fb_row));
|
|
|
|
});
|
|
|
|
return builder.CreateVector(fb_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-15 21:20:22 -05:00
|
|
|
virtual util::json::Array MakeWaypoints(const std::vector<PhantomNode> &phantoms) const
|
|
|
|
{
|
|
|
|
util::json::Array json_waypoints;
|
|
|
|
json_waypoints.values.reserve(phantoms.size());
|
|
|
|
BOOST_ASSERT(phantoms.size() == parameters.coordinates.size());
|
2016-03-08 10:46:01 -05:00
|
|
|
|
2016-05-27 15:05:04 -04:00
|
|
|
boost::range::transform(
|
|
|
|
phantoms,
|
|
|
|
std::back_inserter(json_waypoints.values),
|
|
|
|
[this](const PhantomNode &phantom) { return BaseAPI::MakeWaypoint(phantom); });
|
2016-02-15 21:20:22 -05:00
|
|
|
return json_waypoints;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual util::json::Array MakeWaypoints(const std::vector<PhantomNode> &phantoms,
|
|
|
|
const std::vector<std::size_t> &indices) const
|
|
|
|
{
|
|
|
|
util::json::Array json_waypoints;
|
|
|
|
json_waypoints.values.reserve(indices.size());
|
2016-05-27 15:05:04 -04:00
|
|
|
boost::range::transform(indices,
|
|
|
|
std::back_inserter(json_waypoints.values),
|
|
|
|
[this, phantoms](const std::size_t idx) {
|
2016-03-08 10:46:01 -05:00
|
|
|
BOOST_ASSERT(idx < phantoms.size());
|
|
|
|
return BaseAPI::MakeWaypoint(phantoms[idx]);
|
|
|
|
});
|
2016-02-15 21:20:22 -05:00
|
|
|
return json_waypoints;
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:18:55 -04:00
|
|
|
virtual util::json::Array MakeDurationTable(const std::vector<EdgeWeight> &values,
|
|
|
|
std::size_t number_of_rows,
|
|
|
|
std::size_t number_of_columns) const
|
2016-02-15 21:20:22 -05:00
|
|
|
{
|
|
|
|
util::json::Array json_table;
|
2016-04-12 06:42:16 -04:00
|
|
|
for (const auto row : util::irange<std::size_t>(0UL, number_of_rows))
|
2016-02-15 21:20:22 -05:00
|
|
|
{
|
|
|
|
util::json::Array json_row;
|
|
|
|
auto row_begin_iterator = values.begin() + (row * number_of_columns);
|
|
|
|
auto row_end_iterator = values.begin() + ((row + 1) * number_of_columns);
|
2016-03-03 18:23:38 -05:00
|
|
|
json_row.values.resize(number_of_columns);
|
2016-05-27 15:05:04 -04:00
|
|
|
std::transform(row_begin_iterator,
|
|
|
|
row_end_iterator,
|
|
|
|
json_row.values.begin(),
|
|
|
|
[](const EdgeWeight duration) {
|
2017-01-19 16:52:09 -05:00
|
|
|
if (duration == MAXIMAL_EDGE_DURATION)
|
2016-03-03 08:26:13 -05:00
|
|
|
{
|
|
|
|
return util::json::Value(util::json::Null());
|
|
|
|
}
|
2018-04-20 18:18:55 -04:00
|
|
|
// division by 10 because the duration is in deciseconds (10s)
|
2016-03-03 08:26:13 -05:00
|
|
|
return util::json::Value(util::json::Number(duration / 10.));
|
|
|
|
});
|
2016-02-15 21:20:22 -05:00
|
|
|
json_table.values.push_back(std::move(json_row));
|
|
|
|
}
|
|
|
|
return json_table;
|
|
|
|
}
|
|
|
|
|
2018-04-20 18:18:55 -04:00
|
|
|
virtual util::json::Array MakeDistanceTable(const std::vector<EdgeDistance> &values,
|
|
|
|
std::size_t number_of_rows,
|
|
|
|
std::size_t number_of_columns) const
|
|
|
|
{
|
|
|
|
util::json::Array json_table;
|
|
|
|
for (const auto row : util::irange<std::size_t>(0UL, number_of_rows))
|
|
|
|
{
|
|
|
|
util::json::Array json_row;
|
|
|
|
auto row_begin_iterator = values.begin() + (row * number_of_columns);
|
|
|
|
auto row_end_iterator = values.begin() + ((row + 1) * number_of_columns);
|
|
|
|
json_row.values.resize(number_of_columns);
|
|
|
|
std::transform(row_begin_iterator,
|
|
|
|
row_end_iterator,
|
|
|
|
json_row.values.begin(),
|
|
|
|
[](const EdgeDistance distance) {
|
|
|
|
if (distance == INVALID_EDGE_DISTANCE)
|
|
|
|
{
|
|
|
|
return util::json::Value(util::json::Null());
|
|
|
|
}
|
|
|
|
// round to single decimal place
|
|
|
|
return util::json::Value(
|
|
|
|
util::json::Number(std::round(distance * 10) / 10.));
|
|
|
|
});
|
|
|
|
json_table.values.push_back(std::move(json_row));
|
|
|
|
}
|
|
|
|
return json_table;
|
|
|
|
}
|
|
|
|
|
2018-12-11 12:21:57 -05:00
|
|
|
virtual util::json::Array
|
|
|
|
MakeEstimatesTable(const std::vector<TableCellRef> &fallback_speed_cells) const
|
|
|
|
{
|
|
|
|
util::json::Array json_table;
|
|
|
|
std::for_each(
|
|
|
|
fallback_speed_cells.begin(), fallback_speed_cells.end(), [&](const auto &cell) {
|
|
|
|
util::json::Array row;
|
|
|
|
row.values.push_back(util::json::Number(cell.row));
|
|
|
|
row.values.push_back(util::json::Number(cell.column));
|
|
|
|
json_table.values.push_back(std::move(row));
|
|
|
|
});
|
|
|
|
return json_table;
|
|
|
|
}
|
|
|
|
|
2016-02-15 21:20:22 -05:00
|
|
|
const TableParameters ¶meters;
|
|
|
|
};
|
|
|
|
|
2016-02-17 19:41:50 -05:00
|
|
|
} // ns api
|
|
|
|
} // ns engine
|
|
|
|
} // ns osrm
|
2016-02-15 21:20:22 -05:00
|
|
|
|
|
|
|
#endif
|