15 #ifndef TRAFFIC_SIMULATOR__LANELET_WRAPPER_HPP_
16 #define TRAFFIC_SIMULATOR__LANELET_WRAPPER_HPP_
18 #include <lanelet2_routing/RoutingGraph.h>
19 #include <lanelet2_routing/RoutingGraphContainer.h>
20 #include <lanelet2_traffic_rules/TrafficRulesFactory.h>
22 #include <autoware_lanelet2_extension/utility/utilities.hpp>
25 #include <geometry_msgs/msg/point.hpp>
26 #include <geometry_msgs/msg/pose.hpp>
27 #include <geometry_msgs/msg/pose_stamped.hpp>
28 #include <geometry_msgs/msg/vector3.hpp>
34 #include <traffic_simulator_msgs/msg/bounding_box.hpp>
35 #include <traffic_simulator_msgs/msg/entity_type.hpp>
36 #include <traffic_simulator_msgs/msg/lanelet_pose.hpp>
41 struct hash<
std::tuple<lanelet::Id, lanelet::Id, bool>>
44 size_t operator()(
const std::tuple<lanelet::Id, lanelet::Id, bool> & data)
const
46 std::hash<lanelet::Id> lanelet_id_hash;
49 seed ^= lanelet_id_hash(std::get<0>(data)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
50 seed ^= lanelet_id_hash(std::get<1>(data)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
51 seed ^= std::hash<bool>{}(std::get<2>(data)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
59 namespace lanelet_wrapper
74 const lanelet::Id from_lanelet_id,
const lanelet::Id to_lanelet_id,
76 const lanelet::routing::RoutingGraphConstPtr & routing_graph) -> lanelet::Ids
78 if (!exists(from_lanelet_id, to_lanelet_id, routing_configuration.allow_lane_change)) {
80 constexpr
int routing_cost_id = 0;
81 const auto & from_lanelet = lanelet_map->laneletLayer.get(from_lanelet_id);
82 const auto & to_lanelet = lanelet_map->laneletLayer.get(to_lanelet_id);
83 if (
const auto route = routing_graph->getRoute(
84 from_lanelet, to_lanelet, routing_cost_id, routing_configuration.allow_lane_change);
87 from_lanelet_id, to_lanelet_id, routing_configuration.allow_lane_change, lanelet::Ids());
89 lanelet::Ids shortest_path_ids;
90 for (
const auto & lanelet :
route->shortestPath()) {
91 shortest_path_ids.push_back(lanelet.id());
94 from_lanelet_id, to_lanelet_id, routing_configuration.allow_lane_change,
98 return readData(from_lanelet_id, to_lanelet_id, routing_configuration.allow_lane_change);
101 auto getRoute(
const lanelet::Id from,
const lanelet::Id to,
const bool allow_lane_change)
104 if (!exists(from, to, allow_lane_change)) {
106 "route from : ", from,
" to : ", to, (allow_lane_change ?
" with" :
" without"),
107 " lane change does not exists on route cache.");
109 return readData(from, to, allow_lane_change);
114 auto exists(
const lanelet::Id from,
const lanelet::Id to,
const bool allow_lane_change) ->
bool
116 std::lock_guard lock(mutex_);
117 std::tuple<lanelet::Id, lanelet::Id, bool> key = {from, to, allow_lane_change};
118 return data_.find(key) != data_.end();
121 auto readData(
const lanelet::Id from,
const lanelet::Id to,
const bool allow_lane_change)
124 std::lock_guard lock(mutex_);
125 return data_.at({from, to, allow_lane_change});
129 const lanelet::Id from,
const lanelet::Id to,
const bool allow_lane_change,
130 const lanelet::Ids &
route) ->
void
132 std::lock_guard lock(mutex_);
133 data_[{from, to, allow_lane_change}] =
route;
136 std::unordered_map<std::tuple<lanelet::Id, lanelet::Id, bool>, lanelet::Ids> data_;
145 if (!exists(lanelet_id)) {
148 return readData(lanelet_id);
153 if (!exists(lanelet_id)) {
156 return readDataSpline(lanelet_id);
159 auto getCenterPoints(
const lanelet::Id lanelet_id,
const lanelet::LaneletMapPtr & lanelet_map)
160 -> std::vector<Point>
162 if (!exists(lanelet_id)) {
163 appendData(lanelet_id,
centerPoints(lanelet_id, lanelet_map));
165 return readData(lanelet_id);
169 const lanelet::Id lanelet_id,
const lanelet::LaneletMapPtr & lanelet_map)
170 -> std::shared_ptr<Spline>
172 if (!exists(lanelet_id)) {
173 appendData(lanelet_id,
centerPoints(lanelet_id, lanelet_map));
175 return readDataSpline(lanelet_id);
179 auto exists(
const lanelet::Id lanelet_id) ->
bool
181 std::lock_guard lock(mutex_);
182 return data_.find(lanelet_id) != data_.end();
185 auto readData(
const lanelet::Id lanelet_id) -> std::vector<Point>
187 std::lock_guard lock(mutex_);
188 return data_.at(lanelet_id);
191 auto readDataSpline(
const lanelet::Id lanelet_id) -> std::shared_ptr<Spline>
193 std::lock_guard lock(mutex_);
194 return splines_.at(lanelet_id);
197 auto appendData(
const lanelet::Id lanelet_id,
const std::vector<Point> &
route) ->
void
199 std::lock_guard lock(mutex_);
200 data_[lanelet_id] =
route;
201 splines_[lanelet_id] = std::make_shared<Spline>(
route);
204 auto centerPoints(
const lanelet::Id lanelet_id,
const lanelet::LaneletMapPtr & lanelet_map)
const
205 -> std::vector<Point>
207 std::vector<Point> center_points;
208 for (
const auto & point : lanelet_map->laneletLayer.get(lanelet_id).centerline()) {
209 center_points.push_back(geometry_msgs::build<Point>().x(point.x()).y(point.y()).z(point.z()));
211 if (center_points.size() == 2) {
212 const auto p0 = center_points[0];
213 const auto p2 = center_points[1];
214 const auto p1 = geometry_msgs::build<Point>()
215 .x((p0.x + p2.x) * 0.5)
216 .y((p0.y + p2.y) * 0.5)
217 .z((p0.z + p2.z) * 0.5);
218 center_points.clear();
219 center_points.push_back(p0);
220 center_points.push_back(p1);
221 center_points.push_back(p2);
223 return center_points;
226 std::unordered_map<lanelet::Id, std::vector<Point>> data_;
227 std::unordered_map<lanelet::Id, std::shared_ptr<Spline>> splines_;
236 if (!exists(lanelet_id)) {
239 return readData(lanelet_id);
242 auto getLength(
const lanelet::Id lanelet_id,
const lanelet::LaneletMapPtr & lanelet_map) ->
double
244 if (!exists(lanelet_id)) {
246 lanelet_id, lanelet::utils::getLaneletLength2d(lanelet_map->laneletLayer.get(lanelet_id)));
248 return readData(lanelet_id);
252 auto exists(
const lanelet::Id lanelet_id) ->
bool
254 std::lock_guard lock(mutex_);
255 return data_.find(lanelet_id) != data_.end();
258 auto readData(
const lanelet::Id lanelet_id) ->
double
260 std::lock_guard lock(mutex_);
261 return data_.at(lanelet_id);
264 auto appendData(
const lanelet::Id lanelet_id,
double length) ->
void
266 std::lock_guard lock(mutex_);
267 data_[lanelet_id] = length;
270 std::unordered_map<lanelet::Id, double> data_;
276 lanelet::traffic_rules::TrafficRulesPtr
rules;
277 lanelet::routing::RoutingGraphConstPtr
graph;
281 const lanelet::LaneletMapPtr lanelet_map_ptr,
const std::string & locations,
284 rules = lanelet::traffic_rules::TrafficRulesFactory::create(locations, participants);
285 graph = lanelet::routing::RoutingGraph::build(*lanelet_map_ptr, *
rules);
294 [[nodiscard]]
static auto map() -> lanelet::LaneletMapPtr;
296 -> lanelet::routing::RoutingGraphConstPtr;
298 -> lanelet::traffic_rules::TrafficRulesPtr;
305 explicit LaneletWrapper(
const std::filesystem::path & lanelet_map_path);
308 inline static std::unique_ptr<LaneletWrapper> instance{
nullptr};
310 inline static std::mutex mutex_;
312 const lanelet::LaneletMapPtr lanelet_map_ptr_;
Definition: catmull_rom_spline.hpp:32
Definition: lanelet_wrapper.hpp:141
auto getCenterPointsSpline(const lanelet::Id lanelet_id, const lanelet::LaneletMapPtr &lanelet_map) -> std::shared_ptr< Spline >
Definition: lanelet_wrapper.hpp:168
auto centerPointsSpline(lanelet::Id lanelet_id) -> decltype(auto)
Definition: lanelet_wrapper.hpp:151
auto centerPoints(lanelet::Id lanelet_id) -> decltype(auto)
Definition: lanelet_wrapper.hpp:143
auto getCenterPoints(const lanelet::Id lanelet_id, const lanelet::LaneletMapPtr &lanelet_map) -> std::vector< Point >
Definition: lanelet_wrapper.hpp:159
Definition: lanelet_wrapper.hpp:232
auto getLength(lanelet::Id lanelet_id)
Definition: lanelet_wrapper.hpp:234
auto getLength(const lanelet::Id lanelet_id, const lanelet::LaneletMapPtr &lanelet_map) -> double
Definition: lanelet_wrapper.hpp:242
Definition: lanelet_wrapper.hpp:290
static auto routingGraph(const RoutingGraphType type) -> lanelet::routing::RoutingGraphConstPtr
Definition: lanelet_wrapper.cpp:34
static auto centerPointsCache() -> CenterPointsCache &
Definition: lanelet_wrapper.cpp:84
static auto routeCache(const RoutingGraphType type) -> RouteCache &
Definition: lanelet_wrapper.cpp:68
static auto activate(const std::string &lanelet_map_path) -> void
Definition: lanelet_wrapper.cpp:23
static auto map() -> lanelet::LaneletMapPtr
Definition: lanelet_wrapper.cpp:32
static auto laneletLengthCache() -> LaneletLengthCache &
Definition: lanelet_wrapper.cpp:89
static auto trafficRules(const RoutingGraphType type) -> lanelet::traffic_rules::TrafficRulesPtr
Definition: lanelet_wrapper.cpp:51
Definition: lanelet_wrapper.hpp:71
auto getRoute(const lanelet::Id from_lanelet_id, const lanelet::Id to_lanelet_id, const lanelet::LaneletMapPtr &lanelet_map, const RoutingConfiguration &routing_configuration, const lanelet::routing::RoutingGraphConstPtr &routing_graph) -> lanelet::Ids
Definition: lanelet_wrapper.hpp:73
auto getRoute(const lanelet::Id from, const lanelet::Id to, const bool allow_lane_change) -> decltype(auto)
Definition: lanelet_wrapper.hpp:101
#define THROW_SIMULATION_ERROR(...)
Definition: exception.hpp:60
Definition: lanelet_wrapper.hpp:39
geometry_msgs::msg::Pose Pose
Definition: lanelet_wrapper.hpp:65
traffic_simulator_msgs::msg::BoundingBox BoundingBox
Definition: lanelet_wrapper.hpp:61
traffic_simulator_msgs::msg::LaneletPose LaneletPose
Definition: lanelet_wrapper.hpp:63
geometry_msgs::msg::Point Point
Definition: lanelet_wrapper.hpp:64
traffic_simulator_msgs::msg::EntityType EntityType
Definition: lanelet_wrapper.hpp:62
geometry_msgs::msg::PoseStamped PoseStamped
Definition: lanelet_wrapper.hpp:66
geometry_msgs::msg::Vector3 Vector3
Definition: lanelet_wrapper.hpp:68
auto route(Ts &&... xs)
Definition: route.hpp:30
RoutingGraphType
Definition: routing_graph_type.hpp:24
std::string string
Definition: junit5.hpp:26
traffic_simulator_msgs::EntityType EntityType
Definition: helper_functions.hpp:31
size_t operator()(const std::tuple< lanelet::Id, lanelet::Id, bool > &data) const
Definition: lanelet_wrapper.hpp:44
Definition: routing_configuration.hpp:24
Definition: lanelet_wrapper.hpp:275
RouteCache route_cache
Definition: lanelet_wrapper.hpp:278
TrafficRulesWithRoutingGraph(const lanelet::LaneletMapPtr lanelet_map_ptr, const std::string &locations, const std::string &participants)
Definition: lanelet_wrapper.hpp:280
lanelet::traffic_rules::TrafficRulesPtr rules
Definition: lanelet_wrapper.hpp:276
lanelet::routing::RoutingGraphConstPtr graph
Definition: lanelet_wrapper.hpp:277