/* Copyright (c) 2016, Project OSRM contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef ENGINE_API_TABLE_HPP #define ENGINE_API_TABLE_HPP #include "engine/api/base_api.hpp" #include "engine/api/table_parameters.hpp" #include "engine/api/json_factory.hpp" #include "engine/datafacade/datafacade_base.hpp" #include "engine/guidance/assemble_leg.hpp" #include "engine/guidance/assemble_route.hpp" #include "engine/guidance/assemble_geometry.hpp" #include "engine/guidance/assemble_overview.hpp" #include "engine/guidance/assemble_steps.hpp" #include "engine/internal_route_result.hpp" #include "util/integer_range.hpp" namespace osrm { namespace engine { namespace api { class TableAPI final : public BaseAPI { public: TableAPI(const datafacade::BaseDataFacade &facade_, const TableParameters ¶meters_) : BaseAPI(facade_, parameters_), parameters(parameters_) { } virtual void MakeResponse(const std::vector &durations, const std::vector &phantoms, util::json::Object &response) const { auto number_of_sources = parameters.sources.size(); auto number_of_destinations = parameters.destinations.size(); ; // symmetric case if (parameters.sources.empty()) { response.values["sources"] = MakeWaypoints(phantoms); number_of_sources = phantoms.size(); } else { response.values["sources"] = MakeWaypoints(phantoms, parameters.sources); } if (parameters.destinations.empty()) { response.values["destinations"] = MakeWaypoints(phantoms); number_of_destinations = phantoms.size(); } else { response.values["destinations"] = MakeWaypoints(phantoms, parameters.destinations); } response.values["durations"] = MakeTable(durations, number_of_sources, number_of_destinations); response.values["code"] = "ok"; } protected: virtual util::json::Array MakeWaypoints(const std::vector &phantoms) const { util::json::Array json_waypoints; json_waypoints.values.reserve(phantoms.size()); BOOST_ASSERT(phantoms.size() == parameters.coordinates.size()); auto phantom_iter = phantoms.begin(); auto coordinate_iter = parameters.coordinates.begin(); for (; phantom_iter != phantoms.end() && coordinate_iter != parameters.coordinates.end(); ++phantom_iter, ++coordinate_iter) { json_waypoints.values.push_back(BaseAPI::MakeWaypoint(*coordinate_iter, *phantom_iter)); } return json_waypoints; } virtual util::json::Array MakeWaypoints(const std::vector &phantoms, const std::vector &indices) const { util::json::Array json_waypoints; json_waypoints.values.reserve(indices.size()); for (auto idx : indices) { BOOST_ASSERT(idx < phantoms.size() && idx < parameters.coordinates.size()); json_waypoints.values.push_back( BaseAPI::MakeWaypoint(parameters.coordinates[idx], phantoms[idx])); } return json_waypoints; } virtual util::json::Array MakeTable(const std::vector &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(0, 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 EdgeWeight duration) { if (duration == INVALID_EDGE_WEIGHT) { return util::json::Value(util::json::Null()); } return util::json::Value(util::json::Number(duration / 10.)); }); json_table.values.push_back(std::move(json_row)); } return json_table; } const TableParameters ¶meters; }; } // ns api } // ns engine } // ns osrm #endif