Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
IndexedDistanceMap.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 LEIDOS.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
18
19namespace carma_wm
20{
21void IndexedDistanceMap::pushBack(const lanelet::LineString2d& ls)
22{
23 if (id_index_map.find(ls.id()) != id_index_map.end())
24 {
25 throw std::invalid_argument("IndexedDistanceMap already contains this ls");
26 }
27 std::vector<double> accumulated_dist;
28 accumulated_dist.reserve(ls.size());
29 accumulated_dist.push_back(0);
30 size_t ls_i = accum_lengths.size();
31 id_index_map[ls.front().id()] = std::make_pair(ls_i, 0); // Add first point to id map
32 for (size_t i = 0; i < ls.numSegments(); i++)
33 {
34 auto segment = ls.segment(i);
35 double dist = lanelet::geometry::distance2d(segment.first, segment.second); // length of line string
36 accumulated_dist.push_back(dist + accumulated_dist.back()); // Distance along linestring
37 id_index_map[segment.second.id()] = std::make_pair(ls_i, i + 1); // Add point id and index to map
38 }
39 auto tuple = std::make_tuple(accumulated_dist, totalLength());
40 accum_lengths.push_back(tuple);
41 id_index_map[ls.id()] = std::make_pair(ls_i, 0); // Add linestirng id
42}
43
44std::pair<size_t, size_t> IndexedDistanceMap::getElementIndexByDistance(double distance, bool get_point) const {
45 if (distance < 0) {
46 throw std::invalid_argument("Distance must non-negative: " + std::to_string(distance));
47 }
48 if (distance > totalLength()) {
49 throw std::invalid_argument("Distance cannot be greater than distance map length");
50 }
51 if (accum_lengths.size() == 0) {
52 throw std::invalid_argument("No data available in distance map");
53 }
54
55 auto low = std::lower_bound (accum_lengths.begin(), accum_lengths.end(), distance, // Binary search to find the index
56 [](const std::tuple<std::vector<double>, double>& a, const double& b){return std::get<1>(a) < b;});
57
58 size_t ls_index = 0;
59 if (low == accum_lengths.end()) { // If we reached the end, it means we should pick the final point since we already checked the bounds
60 ls_index = accum_lengths.size() - 1;
61 } else {
62 ls_index = std::max(low - accum_lengths.begin() - 1, (long int)0);
63 }
64
65 if (!get_point) { // If we are looking for the linestring index then return
66 return std::make_pair(ls_index, (size_t) 0);
67 }
68
69 // If we are looking for the point index then calculate the index in the same manner as before
70 double dist_to_ls = distanceToElement(ls_index);
71 double relative_dist = distance - dist_to_ls;
72
73 auto low_point = std::lower_bound (std::get<0>(accum_lengths[ls_index]).begin(), std::get<0>(accum_lengths[ls_index]).end(), relative_dist, // Binary search to find the point index
74 [](const double& a, const double& b){ return a < b; });
75
76 size_t point_idx = std::max(low_point - std::get<0>(accum_lengths[ls_index]).begin() - 1, (long int)0);
77 return std::make_pair(ls_index, point_idx);
78}
79
80double IndexedDistanceMap::elementLength(size_t index) const
81{
82 return std::get<0>(accum_lengths[index]).back();
83}
84
85double IndexedDistanceMap::distanceToElement(size_t index) const
86{
87 return std::get<1>(accum_lengths[index]);
88}
89
90double IndexedDistanceMap::distanceBetween(size_t index, size_t p1_index, size_t p2_index) const
91{
92 return fabs(distanceToPointAlongElement(index, p2_index) - distanceToPointAlongElement(index, p1_index));
93}
94
95double IndexedDistanceMap::distanceToPointAlongElement(size_t index, size_t point_index) const
96{
97 return std::get<0>(accum_lengths[index])[point_index];
98}
99
101{
102 if (accum_lengths.size() == 0)
103 {
104 return 0.0;
105 }
106 return distanceToElement(accum_lengths.size() - 1) + elementLength(accum_lengths.size() - 1);
107}
108
109std::pair<size_t, size_t> IndexedDistanceMap::getIndexFromId(const lanelet::Id& id) const
110{
111 return id_index_map.at(id);
112}
113
115{
116 return accum_lengths.size();
117}
118
119size_t IndexedDistanceMap::size(size_t index) const
120{
121 return std::get<0>(accum_lengths[index]).size();
122}
123
124} // namespace carma_wm
std::pair< size_t, size_t > getIndexFromId(const lanelet::Id &id) const
Returns the indexes of the element identified by the provided Id NOTE: It is up to the user to know i...
std::unordered_map< lanelet::Id, std::pair< size_t, size_t > > id_index_map
void pushBack(const lanelet::LineString2d &ls)
Add a linestring to this structure. This function will iterate over the line string to compute distan...
double elementLength(size_t index) const
Get the length of the linestring located at the provided index.
std::vector< std::tuple< std::vector< double >, double > > accum_lengths
std::pair< size_t, size_t > getElementIndexByDistance(double distance, bool get_point=true) const
Returns index of the linestring which the provided distance is within. NOTE: Unlike the rest of this ...
double distanceToPointAlongElement(size_t index, size_t point_index) const
Get the along-line distance to the point on the provided linestring.
double totalLength() const
Returns the total along-line length of this structure.
double distanceToElement(size_t index) const
Get the distance to the start of the linestring at the specified index.
size_t size() const
Returns number of linestrings in this structure.
double distanceBetween(size_t index, size_t p1_index, size_t p2_index) const
Get the distance between two points on the same linestring.
auto to_string(const UtmZone &zone) -> std::string
Definition: utm_zone.cpp:21