scenario_simulator_v2 C++ API
cache.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TRAFFIC_SIMULATOR__HDMAP_UTILS__CACHE_HPP_
16 #define TRAFFIC_SIMULATOR__HDMAP_UTILS__CACHE_HPP_
17 
19 #include <geometry_msgs/msg/point.hpp>
20 #include <mutex>
21 #include <optional>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace std
27 {
28 template <>
29 struct hash<std::tuple<lanelet::Id, lanelet::Id, bool>>
30 {
31 public:
32  size_t operator()(const std::tuple<lanelet::Id, lanelet::Id, bool> & data) const
33  {
34  std::hash<lanelet::Id> lanelet_id_hash;
35  size_t seed = 0;
36  // hash combine like boost library
37  seed ^= lanelet_id_hash(std::get<0>(data)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
38  seed ^= lanelet_id_hash(std::get<1>(data)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
39  seed ^= std::hash<bool>{}(std::get<2>(data)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
40  return seed;
41  }
42 };
43 } // namespace std
44 
45 namespace hdmap_utils
46 {
48 {
49 public:
50  auto exists(lanelet::Id from, lanelet::Id to, bool allow_lane_change)
51  {
52  std::lock_guard<std::mutex> lock(mutex_);
53  std::tuple<lanelet::Id, lanelet::Id, bool> key = {from, to, allow_lane_change};
54  return data_.find(key) != data_.end();
55  }
56 
57  auto getRoute(const lanelet::Id from, const lanelet::Id to, const bool allow_lane_change)
58  -> decltype(auto)
59  {
60  if (!exists(from, to, allow_lane_change)) {
62  "route from : ", from, " to : ", to, (allow_lane_change ? " with" : " without"),
63  " lane change does not exists on route cache.");
64  } else {
65  std::lock_guard<std::mutex> lock(mutex_);
66  return data_.at({from, to, allow_lane_change});
67  }
68  }
69 
70  auto appendData(
71  lanelet::Id from, lanelet::Id to, const bool allow_lane_change, const lanelet::Ids & route)
72  -> void
73  {
74  std::lock_guard<std::mutex> lock(mutex_);
75  data_[{from, to, allow_lane_change}] = route;
76  }
77 
78 private:
79  std::unordered_map<std::tuple<lanelet::Id, lanelet::Id, bool>, lanelet::Ids> data_;
80 
81  std::mutex mutex_;
82 };
83 
85 {
86 public:
87  auto exists(lanelet::Id lanelet_id) -> bool
88  {
89  std::lock_guard<std::mutex> lock(mutex_);
90  return data_.find(lanelet_id) != data_.end();
91  }
92 
93  auto getCenterPoints(lanelet::Id lanelet_id) -> decltype(auto)
94  {
95  if (!exists(lanelet_id)) {
96  THROW_SIMULATION_ERROR("center point of : ", lanelet_id, " does not exists on route cache.");
97  }
98  std::lock_guard<std::mutex> lock(mutex_);
99  return data_.at(lanelet_id);
100  }
101 
102  auto getCenterPointsSpline(lanelet::Id lanelet_id) -> decltype(auto)
103  {
104  if (!exists(lanelet_id)) {
105  THROW_SIMULATION_ERROR("center point of : ", lanelet_id, " does not exists on route cache.");
106  }
107  std::lock_guard<std::mutex> lock(mutex_);
108  return splines_[lanelet_id];
109  }
110 
111  auto appendData(lanelet::Id lanelet_id, const std::vector<geometry_msgs::msg::Point> & route)
112  {
113  std::lock_guard<std::mutex> lock(mutex_);
114  data_[lanelet_id] = route;
115  splines_[lanelet_id] = std::make_shared<math::geometry::CatmullRomSpline>(route);
116  }
117 
118 private:
119  std::unordered_map<lanelet::Id, std::vector<geometry_msgs::msg::Point>> data_;
120 
121  std::unordered_map<lanelet::Id, std::shared_ptr<math::geometry::CatmullRomSpline>> splines_;
122 
123  std::mutex mutex_;
124 };
125 
127 {
128 public:
129  auto exists(lanelet::Id lanelet_id)
130  {
131  std::lock_guard<std::mutex> lock(mutex_);
132  return data_.find(lanelet_id) != data_.end();
133  }
134 
135  auto getLength(lanelet::Id lanelet_id)
136  {
137  if (!exists(lanelet_id)) {
138  THROW_SIMULATION_ERROR("length of : ", lanelet_id, " does not exists on route cache.");
139  }
140  std::lock_guard<std::mutex> lock(mutex_);
141  return data_[lanelet_id];
142  }
143 
144  auto appendData(lanelet::Id lanelet_id, double length)
145  {
146  std::lock_guard<std::mutex> lock(mutex_);
147  data_[lanelet_id] = length;
148  }
149 
150 private:
151  std::unordered_map<lanelet::Id, double> data_;
152 
153  std::mutex mutex_;
154 };
155 } // namespace hdmap_utils
156 
157 #endif // TRAFFIC_SIMULATOR__HDMAP_UTILS__CACHE_HPP_
Definition: cache.hpp:85
auto getCenterPoints(lanelet::Id lanelet_id) -> decltype(auto)
Definition: cache.hpp:93
auto getCenterPointsSpline(lanelet::Id lanelet_id) -> decltype(auto)
Definition: cache.hpp:102
auto appendData(lanelet::Id lanelet_id, const std::vector< geometry_msgs::msg::Point > &route)
Definition: cache.hpp:111
auto exists(lanelet::Id lanelet_id) -> bool
Definition: cache.hpp:87
Definition: cache.hpp:127
auto appendData(lanelet::Id lanelet_id, double length)
Definition: cache.hpp:144
auto getLength(lanelet::Id lanelet_id)
Definition: cache.hpp:135
auto exists(lanelet::Id lanelet_id)
Definition: cache.hpp:129
Definition: cache.hpp:48
auto exists(lanelet::Id from, lanelet::Id to, bool allow_lane_change)
Definition: cache.hpp:50
auto appendData(lanelet::Id from, lanelet::Id to, const bool allow_lane_change, const lanelet::Ids &route) -> void
Definition: cache.hpp:70
auto getRoute(const lanelet::Id from, const lanelet::Id to, const bool allow_lane_change) -> decltype(auto)
Definition: cache.hpp:57
#define THROW_SIMULATION_ERROR(...)
Definition: exception.hpp:60
Definition: cache.hpp:46
Definition: cache.hpp:27
size_t operator()(const std::tuple< lanelet::Id, lanelet::Id, bool > &data) const
Definition: cache.hpp:32