-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathtable.cpp
More file actions
164 lines (136 loc) · 6.66 KB
/
table.cpp
File metadata and controls
164 lines (136 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "engine/plugins/table.hpp"
#include "engine/api/table_api.hpp"
#include "engine/api/table_parameters.hpp"
#include "util/coordinate_calculation.hpp"
#include "util/string_util.hpp"
#include <cstdlib>
#include <vector>
#include <boost/assert.hpp>
namespace osrm::engine::plugins
{
TablePlugin::TablePlugin(const int max_locations_distance_table,
const std::optional<double> default_radius)
: BasePlugin(default_radius), max_locations_distance_table(max_locations_distance_table)
{
}
Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
const api::TableParameters ¶ms,
osrm::engine::api::ResultT &result) const
{
if (!algorithms.HasManyToManySearch())
{
return Error("NotImplemented",
"Many to many search is not implemented for the chosen search algorithm.",
result);
}
BOOST_ASSERT(params.IsValid());
if (!CheckAllCoordinates(params.coordinates))
{
return Error("InvalidOptions", "Coordinates are invalid", result);
}
if (!params.bearings.empty() && params.coordinates.size() != params.bearings.size())
{
return Error(
"InvalidOptions", "Number of bearings does not match number of coordinates", result);
}
if (!params.bearings.empty() && !default_radius.has_value() &&
params.radiuses.size() != params.bearings.size())
{
return Error(
"InvalidOptions", "Number of radiuses does not match number of bearings", result);
}
// Empty sources or destinations means the user wants all of them included, respectively
// The ManyToMany routing algorithm we dispatch to below already handles this perfectly.
const auto num_sources =
params.sources.empty() ? params.coordinates.size() : params.sources.size();
const auto num_destinations =
params.destinations.empty() ? params.coordinates.size() : params.destinations.size();
if (max_locations_distance_table > 0 &&
((num_sources * num_destinations) >
static_cast<std::size_t>(max_locations_distance_table * max_locations_distance_table)))
{
return Error("TooBig", "Too many table coordinates", result);
}
if (!CheckAlgorithms(params, algorithms, result))
return Status::Error;
const auto &facade = algorithms.GetFacade();
auto phantom_nodes = GetPhantomNodes(facade, params);
if (phantom_nodes.size() != params.coordinates.size())
{
return Error(
"NoSegment", MissingPhantomErrorMessage(phantom_nodes, params.coordinates), result);
}
auto snapped_phantoms = SnapPhantomNodes(std::move(phantom_nodes));
bool request_distance = params.annotations & api::TableParameters::AnnotationsType::Distance;
bool request_duration = params.annotations & api::TableParameters::AnnotationsType::Duration;
auto result_tables_pair = algorithms.ManyToManySearch(
snapped_phantoms, params.sources, params.destinations, request_distance);
if ((request_duration && result_tables_pair.first.empty()) ||
(request_distance && result_tables_pair.second.empty()))
{
return Error("NoTable", "No table found", result);
}
std::vector<api::TableAPI::TableCellRef> estimated_pairs;
// Scan table for null results - if any exist, replace with distance estimates
if (params.fallback_speed != from_alias<double>(INVALID_FALLBACK_SPEED) ||
params.scale_factor != 1)
{
for (std::size_t row = 0; row < num_sources; row++)
{
for (std::size_t column = 0; column < num_destinations; column++)
{
const auto &table_index = row * num_destinations + column;
BOOST_ASSERT(table_index < result_tables_pair.first.size());
if (params.fallback_speed != from_alias<double>(INVALID_FALLBACK_SPEED) &&
params.fallback_speed > 0 &&
result_tables_pair.first[table_index] == MAXIMAL_EDGE_DURATION)
{
const auto &source =
snapped_phantoms[params.sources.empty() ? row : params.sources[row]];
const auto &destination =
snapped_phantoms[params.destinations.empty() ? column
: params.destinations[column]];
auto distance_estimate =
params.fallback_coordinate_type ==
api::TableParameters::FallbackCoordinateType::Input
? util::coordinate_calculation::greatCircleDistance(
candidatesInputLocation(source),
candidatesInputLocation(destination))
: util::coordinate_calculation::greatCircleDistance(
candidatesSnappedLocation(source),
candidatesSnappedLocation(destination));
result_tables_pair.first[table_index] =
to_alias<EdgeDuration>(distance_estimate / params.fallback_speed);
if (!result_tables_pair.second.empty())
{
result_tables_pair.second[table_index] =
to_alias<EdgeDistance>(distance_estimate);
}
estimated_pairs.emplace_back(row, column);
}
if (params.scale_factor > 0 && params.scale_factor != 1 &&
result_tables_pair.first[table_index] != MAXIMAL_EDGE_DURATION &&
result_tables_pair.first[table_index] != EdgeDuration{0})
{
EdgeDuration diff =
MAXIMAL_EDGE_DURATION / result_tables_pair.first[table_index];
if (params.scale_factor >= from_alias<double>(diff))
{
result_tables_pair.first[table_index] =
MAXIMAL_EDGE_DURATION - EdgeDuration{1};
}
else
{
result_tables_pair.first[table_index] = to_alias<EdgeDuration>(
std::lround(from_alias<double>(result_tables_pair.first[table_index]) *
params.scale_factor));
}
}
}
}
}
api::TableAPI table_api{facade, params};
table_api.MakeResponse(result_tables_pair, snapped_phantoms, estimated_pairs, result);
return Status::Ok;
}
} // namespace osrm::engine::plugins