scenario_simulator_v2 C++ API
hdmap_utils.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__HDMAP_UTILS_HPP_
16 #define TRAFFIC_SIMULATOR__HDMAP_UTILS__HDMAP_UTILS_HPP_
17 
18 #include <geometry_msgs/msg/vector3.h>
19 #include <lanelet2_core/LaneletMap.h>
20 #include <lanelet2_core/geometry/Lanelet.h>
21 #include <lanelet2_core/primitives/BasicRegulatoryElements.h>
22 #include <lanelet2_core/primitives/LaneletSequence.h>
23 #include <lanelet2_matching/LaneletMatching.h>
24 #include <lanelet2_routing/Route.h>
25 #include <lanelet2_routing/RoutingCost.h>
26 #include <lanelet2_routing/RoutingGraph.h>
27 #include <lanelet2_routing/RoutingGraphContainer.h>
28 #include <lanelet2_traffic_rules/TrafficRulesFactory.h>
29 #include <tf2/LinearMath/Matrix3x3.h>
30 
31 #include <autoware_lanelet2_extension/utility/message_conversion.hpp>
32 #include <autoware_lanelet2_extension/utility/query.hpp>
33 #include <autoware_lanelet2_extension/utility/utilities.hpp>
34 #include <autoware_map_msgs/msg/lanelet_map_bin.hpp>
35 #include <boost/filesystem.hpp>
36 #include <geographic_msgs/msg/geo_point.hpp>
40 #include <geometry_msgs/msg/pose_stamped.hpp>
41 #include <map>
42 #include <memory>
43 #include <optional>
44 #include <rclcpp/rclcpp.hpp>
45 #include <string>
46 #include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
51 #include <traffic_simulator_msgs/msg/bounding_box.hpp>
52 #include <traffic_simulator_msgs/msg/entity_status.hpp>
53 #include <tuple>
54 #include <unordered_map>
55 #include <utility>
56 #include <vector>
57 #include <visualization_msgs/msg/marker_array.hpp>
58 
59 namespace hdmap_utils
60 {
61 enum class LaneletType { LANE, CROSSWALK };
62 
64 {
65 public:
66  explicit HdMapUtils(const boost::filesystem::path &, const geographic_msgs::msg::GeoPoint &);
67 
68  auto canChangeLane(
69  const lanelet::Id from, const lanelet::Id to,
71  traffic_simulator::RoutingConfiguration().routing_graph_type) const -> bool;
72 
74  const lanelet::Id, const double s, const lanelet::Ids &,
75  const double forward_distance = 20) const -> std::vector<geometry_msgs::msg::Point>;
76 
77  auto countLaneChanges(
80  const traffic_simulator::RoutingConfiguration & routing_configuration =
81  traffic_simulator::RoutingConfiguration()) const -> std::optional<std::pair<int, int>>;
82 
83  auto filterLaneletIds(const lanelet::Ids &, const char subtype[]) const -> lanelet::Ids;
84 
85  auto generateMarker() const -> visualization_msgs::msg::MarkerArray;
86 
87  auto getCenterPoints(const lanelet::Ids &) const -> std::vector<geometry_msgs::msg::Point>;
88 
89  auto getCenterPoints(const lanelet::Id) const -> std::vector<geometry_msgs::msg::Point>;
90 
91  auto getCenterPointsSpline(const lanelet::Id) const
92  -> std::shared_ptr<math::geometry::CatmullRomSpline>;
93 
95  const geometry_msgs::msg::Pose &, const double distance_thresh = 30.0,
96  const bool include_crosswalk = false) const -> std::optional<lanelet::Id>;
97 
99  const lanelet::Id lanelet_id, const lanelet::Id crossing_lanelet_id) const
100  -> std::optional<double>;
101 
102  auto getConflictingCrosswalkIds(const lanelet::Ids &) const -> lanelet::Ids;
103 
105  const lanelet::Ids &, const traffic_simulator::RoutingGraphType type =
106  traffic_simulator::RoutingConfiguration().routing_graph_type) const
107  -> lanelet::Ids;
108 
110  const lanelet::Id current_lanelet_id, const lanelet::Ids & route, const double horizon = 100,
111  const bool include_current_lanelet_id = true,
113  traffic_simulator::RoutingConfiguration().routing_graph_type) const -> lanelet::Ids;
114 
116  const lanelet::Id, const double distance = 100, const bool include_self = true,
118  traffic_simulator::RoutingConfiguration().routing_graph_type) const -> lanelet::Ids;
119 
121  const geometry_msgs::msg::Pose & from,
122  const traffic_simulator::lane_change::Parameter & lane_change_parameter,
123  const double maximum_curvature_threshold, const double target_trajectory_length,
124  const double forward_distance_threshold) const
125  -> std::optional<std::pair<math::geometry::HermiteCurve, double>>;
126 
129  const traffic_simulator::lane_change::Parameter & lane_change_parameter) const
130  -> std::optional<std::pair<math::geometry::HermiteCurve, double>>;
131 
133  const lanelet::Id, const traffic_simulator::lane_change::Direction,
135  traffic_simulator::RoutingConfiguration().routing_graph_type) const
136  -> std::optional<lanelet::Id>;
137 
139  const lanelet::Id, const traffic_simulator::lane_change::Direction, const std::uint8_t shift,
141  traffic_simulator::RoutingConfiguration().routing_graph_type) const
142  -> std::optional<lanelet::Id>;
143 
144  auto getLaneletIds() const -> lanelet::Ids;
145 
146  auto getLaneletPolygon(const lanelet::Id) const -> std::vector<geometry_msgs::msg::Point>;
147 
148  auto getLanelets(const lanelet::Ids &) const -> lanelet::Lanelets;
149 
150  auto getNearbyLaneletIds(
151  const geometry_msgs::msg::Point &, const double distance_threshold,
152  const bool include_crosswalk, const std::size_t search_count = 5) const -> lanelet::Ids;
153 
154  auto getNearbyLaneletIds(
155  const geometry_msgs::msg::Point &, const double distance_threshold,
156  const std::size_t search_count = 5) const -> lanelet::Ids;
157 
158  auto getPreviousLanelets(
159  const lanelet::Id, const double backward_horizon = 100,
161  traffic_simulator::RoutingConfiguration().routing_graph_type) const -> lanelet::Ids;
162 
163  auto getRightOfWayLaneletIds(const lanelet::Ids &) const
164  -> std::unordered_map<lanelet::Id, lanelet::Ids>;
165 
166  auto getRightOfWayLaneletIds(const lanelet::Id) const -> lanelet::Ids;
167 
168  auto getRoute(
169  const lanelet::Id from, const lanelet::Id to,
170  const traffic_simulator::RoutingConfiguration & routing_configuration =
171  traffic_simulator::RoutingConfiguration()) const -> lanelet::Ids;
172 
173  auto getSpeedLimit(
174  const lanelet::Ids &, const traffic_simulator::RoutingGraphType type =
175  traffic_simulator::RoutingConfiguration().routing_graph_type) const
176  -> double;
177 
178  auto getTangentVector(const lanelet::Id, const double s) const
179  -> std::optional<geometry_msgs::msg::Vector3>;
180 
182  const lanelet::Id traffic_light_id, const std::string &,
183  const bool allow_infer_position = false) const -> std::optional<geometry_msgs::msg::Point>;
184 
185  auto getTrafficLightIds() const -> lanelet::Ids;
186 
187  auto getTrafficLightIdsOnPath(const lanelet::Ids & route_lanelets) const -> lanelet::Ids;
188 
189  auto getTrafficLightRegulatoryElement(const lanelet::Id) const -> lanelet::TrafficLight::Ptr;
190 
191  auto getTrafficLightRegulatoryElementIDsFromTrafficLight(const lanelet::Id) const -> lanelet::Ids;
192 
193  auto getTrafficLightStopLineIds(const lanelet::Id traffic_light_id) const -> lanelet::Ids;
194 
195  auto getTrafficLightStopLinesPoints(const lanelet::Id traffic_light_id) const
196  -> std::vector<std::vector<geometry_msgs::msg::Point>>;
197 
198  auto insertMarkerArray(
199  visualization_msgs::msg::MarkerArray &, const visualization_msgs::msg::MarkerArray &) const
200  -> void;
201 
202  auto isInIntersection(const lanelet::Id) const -> bool;
203 
204  auto isInLanelet(const lanelet::Id, const double s) const -> bool;
205 
206  auto isInRoute(const lanelet::Id, const lanelet::Ids & route) const -> bool;
207 
208  auto isTrafficLight(const lanelet::Id) const -> bool;
209 
210  auto isTrafficLightRegulatoryElement(const lanelet::Id) const -> bool;
211 
212 private:
215  constexpr static double DEFAULT_MATCH_TO_LANE_REDUCTION_RATIO = 0.8;
216 
217 public:
218  auto toMapBin() const -> autoware_map_msgs::msg::LaneletMapBin;
219 
220  auto toMapPoints(const lanelet::Id, const std::vector<double> & s) const
221  -> std::vector<geometry_msgs::msg::Point>;
222 
223 private:
227  // @{
228  mutable CenterPointsCache center_points_cache_;
229  mutable LaneletLengthCache lanelet_length_cache_;
230  // @}
231 
232  lanelet::LaneletMapPtr lanelet_map_ptr_;
233 
234  class RoutingGraphs
235  {
236  public:
237  explicit RoutingGraphs(const lanelet::LaneletMapPtr & lanelet_map);
238 
240  {
241  lanelet::traffic_rules::TrafficRulesPtr rules;
242  lanelet::routing::RoutingGraphPtr graph;
244  };
245 
246  [[nodiscard]] lanelet::routing::RoutingGraphPtr routing_graph(
247  const traffic_simulator::RoutingGraphType type) const;
248 
249  [[nodiscard]] lanelet::traffic_rules::TrafficRulesPtr traffic_rule(
250  const traffic_simulator::RoutingGraphType type) const;
251 
252  [[nodiscard]] auto getRoute(
253  const lanelet::Id from_lanelet_id, const lanelet::Id to_lanelet_id,
254  lanelet::LaneletMapPtr lanelet_map_ptr,
255  const traffic_simulator::RoutingConfiguration & routing_configuration =
256  traffic_simulator::RoutingConfiguration()) -> lanelet::Ids;
257 
258  private:
259  [[nodiscard]] RouteCache & route_cache(const traffic_simulator::RoutingGraphType type);
260 
261  RuleWithGraph vehicle;
262 
263  RuleWithGraph vehicle_with_road_shoulder;
264 
265  RuleWithGraph pedestrian;
266  };
267 
268  std::unique_ptr<RoutingGraphs> routing_graphs_;
269 
270  template <typename Lanelet>
271  auto getLaneletIds(const std::vector<Lanelet> & lanelets) const -> lanelet::Ids
272  {
273  lanelet::Ids ids;
274  std::transform(
275  lanelets.begin(), lanelets.end(), std::back_inserter(ids),
276  [](const auto & lanelet) { return lanelet.id(); });
277  return ids;
278  }
279 
280  auto absoluteHull(
281  const lanelet::BasicPolygon2d & relative_hull, const lanelet::matching::Pose2d &) const
282  -> lanelet::BasicPolygon2d;
283 
284  auto calcEuclidDist(
285  const std::vector<double> & x, const std::vector<double> & y,
286  const std::vector<double> & z) const -> std::vector<double>;
287 
288  auto calculateAccumulatedLengths(const lanelet::ConstLineString3d &) const -> std::vector<double>;
289 
290  auto calculateSegmentDistances(const lanelet::ConstLineString3d &) const -> std::vector<double>;
291 
292  auto excludeSubtypeLanelets(
293  const std::vector<std::pair<double, lanelet::Lanelet>> &, const char subtype[]) const
294  -> std::vector<std::pair<double, lanelet::Lanelet>>;
295 
296  auto filterLanelets(const lanelet::Lanelets &, const char subtype[]) const -> lanelet::Lanelets;
297 
298  auto findNearestIndexPair(
299  const std::vector<double> & accumulated_lengths, const double target_length) const
300  -> std::pair<std::size_t, std::size_t>;
301 
302  auto generateFineCenterline(const lanelet::ConstLanelet &, const double resolution) const
303  -> lanelet::LineString3d;
304 
308  const double tangent_vector_size = 100) const -> math::geometry::HermiteCurve;
309 
310  auto getTrafficLightRegulatoryElementsOnPath(const lanelet::Ids &) const
311  -> std::vector<std::shared_ptr<const lanelet::autoware::AutowareTrafficLight>>;
312 
313  auto getTrafficLights(const lanelet::Id traffic_light_id) const
314  -> std::vector<lanelet::AutowareTrafficLightConstPtr>;
315 
316  auto getTrafficSignRegulatoryElementsOnPath(const lanelet::Ids &) const
317  -> std::vector<std::shared_ptr<const lanelet::TrafficSign>>;
318 
319  auto getTrafficSignRegulatoryElements() const
320  -> std::vector<std::shared_ptr<const lanelet::TrafficSign>>;
321 
322  auto getVectorFromPose(const geometry_msgs::msg::Pose &, const double magnitude) const
323  -> geometry_msgs::msg::Vector3;
324 
325  auto mapCallback(const autoware_map_msgs::msg::LaneletMapBin &) const -> void;
326 
327  auto resamplePoints(const lanelet::ConstLineString3d &, const std::int32_t num_segments) const
328  -> lanelet::BasicPoints3d;
329 
330  auto toPoint2d(const geometry_msgs::msg::Point &) const -> lanelet::BasicPoint2d;
331 
332  auto toPolygon(const lanelet::ConstLineString3d &) const
333  -> std::vector<geometry_msgs::msg::Point>;
334 };
335 } // namespace hdmap_utils
336 
337 #endif // TRAFFIC_SIMULATOR__HDMAP_UTILS__HDMAP_UTILS_HPP_
Definition: cache.hpp:67
Definition: hdmap_utils.hpp:64
auto getTangentVector(const lanelet::Id, const double s) const -> std::optional< geometry_msgs::msg::Vector3 >
Definition: hdmap_utils.cpp:895
auto getRoute(const lanelet::Id from, const lanelet::Id to, const traffic_simulator::RoutingConfiguration &routing_configuration=traffic_simulator::RoutingConfiguration()) const -> lanelet::Ids
Definition: hdmap_utils.cpp:572
auto toMapPoints(const lanelet::Id, const std::vector< double > &s) const -> std::vector< geometry_msgs::msg::Point >
Definition: hdmap_utils.cpp:884
HdMapUtils(const boost::filesystem::path &, const geographic_msgs::msg::GeoPoint &)
Definition: hdmap_utils.cpp:65
auto isInIntersection(const lanelet::Id) const -> bool
Definition: hdmap_utils.cpp:874
auto getCenterPointsSpline(const lanelet::Id) const -> std::shared_ptr< math::geometry::CatmullRomSpline >
Definition: hdmap_utils.cpp:580
auto getTrafficLightStopLinesPoints(const lanelet::Id traffic_light_id) const -> std::vector< std::vector< geometry_msgs::msg::Point >>
Definition: hdmap_utils.cpp:1122
auto getCollisionPointInLaneCoordinate(const lanelet::Id lanelet_id, const lanelet::Id crossing_lanelet_id) const -> std::optional< double >
Definition: hdmap_utils.cpp:204
auto getTrafficLightIds() const -> lanelet::Ids
Definition: hdmap_utils.cpp:640
auto getSpeedLimit(const lanelet::Ids &, const traffic_simulator::RoutingGraphType type=traffic_simulator::RoutingConfiguration().routing_graph_type) const -> double
Definition: hdmap_utils.cpp:395
auto getConflictingLaneIds(const lanelet::Ids &, const traffic_simulator::RoutingGraphType type=traffic_simulator::RoutingConfiguration().routing_graph_type) const -> lanelet::Ids
Definition: hdmap_utils.cpp:253
auto isInRoute(const lanelet::Id, const lanelet::Ids &route) const -> bool
Definition: hdmap_utils.cpp:485
auto canChangeLane(const lanelet::Id from, const lanelet::Id to, const traffic_simulator::RoutingGraphType type=traffic_simulator::RoutingConfiguration().routing_graph_type) const -> bool
Definition: hdmap_utils.cpp:901
auto getRightOfWayLaneletIds(const lanelet::Ids &) const -> std::unordered_map< lanelet::Id, lanelet::Ids >
Definition: hdmap_utils.cpp:1021
auto filterLaneletIds(const lanelet::Ids &, const char subtype[]) const -> lanelet::Ids
Definition: hdmap_utils.cpp:131
auto getLaneChangeTrajectory(const geometry_msgs::msg::Pose &from, const traffic_simulator::lane_change::Parameter &lane_change_parameter, const double maximum_curvature_threshold, const double target_trajectory_length, const double forward_distance_threshold) const -> std::optional< std::pair< math::geometry::HermiteCurve, double >>
Definition: hdmap_utils.cpp:782
auto getTrafficLightRegulatoryElement(const lanelet::Id) const -> lanelet::TrafficLight::Ptr
Definition: hdmap_utils.cpp:1294
auto isTrafficLightRegulatoryElement(const lanelet::Id) const -> bool
Definition: hdmap_utils.cpp:1287
auto clipTrajectoryFromLaneletIds(const lanelet::Id, const double s, const lanelet::Ids &, const double forward_distance=20) const -> std::vector< geometry_msgs::msg::Point >
Definition: hdmap_utils.cpp:265
auto getTrafficLightBulbPosition(const lanelet::Id traffic_light_id, const std::string &, const bool allow_infer_position=false) const -> std::optional< geometry_msgs::msg::Point >
Definition: hdmap_utils.cpp:659
auto getTrafficLightRegulatoryElementIDsFromTrafficLight(const lanelet::Id) const -> lanelet::Ids
Definition: hdmap_utils.cpp:1302
auto getTrafficLightIdsOnPath(const lanelet::Ids &route_lanelets) const -> lanelet::Ids
Definition: hdmap_utils.cpp:1144
auto isTrafficLight(const lanelet::Id) const -> bool
Definition: hdmap_utils.cpp:1273
auto getLaneletIds() const -> lanelet::Ids
Definition: hdmap_utils.cpp:106
auto getFollowingLanelets(const lanelet::Id current_lanelet_id, const lanelet::Ids &route, const double horizon=100, const bool include_current_lanelet_id=true, const traffic_simulator::RoutingGraphType type=traffic_simulator::RoutingConfiguration().routing_graph_type) const -> lanelet::Ids
Definition: hdmap_utils.cpp:490
auto generateMarker() const -> visualization_msgs::msg::MarkerArray
Definition: hdmap_utils.cpp:933
auto getClosestLaneletId(const geometry_msgs::msg::Pose &, const double distance_thresh=30.0, const bool include_crosswalk=false) const -> std::optional< lanelet::Id >
Definition: hdmap_utils.cpp:363
auto getTrafficLightStopLineIds(const lanelet::Id traffic_light_id) const -> lanelet::Ids
Definition: hdmap_utils.cpp:1110
auto getCenterPoints(const lanelet::Ids &) const -> std::vector< geometry_msgs::msg::Point >
Definition: hdmap_utils.cpp:587
auto getLaneletPolygon(const lanelet::Id) const -> std::vector< geometry_msgs::msg::Point >
Definition: hdmap_utils.cpp:115
auto toMapBin() const -> autoware_map_msgs::msg::LaneletMapBin
Definition: hdmap_utils.cpp:910
auto getLaneChangeableLaneletId(const lanelet::Id, const traffic_simulator::lane_change::Direction, const traffic_simulator::RoutingGraphType type=traffic_simulator::RoutingConfiguration().routing_graph_type) const -> std::optional< lanelet::Id >
Definition: hdmap_utils.cpp:435
auto getPreviousLanelets(const lanelet::Id, const double backward_horizon=100, const traffic_simulator::RoutingGraphType type=traffic_simulator::RoutingConfiguration().routing_graph_type) const -> lanelet::Ids
Definition: hdmap_utils.cpp:459
auto getLanelets(const lanelet::Ids &) const -> lanelet::Lanelets
Definition: hdmap_utils.cpp:1264
auto isInLanelet(const lanelet::Id, const double s) const -> bool
Definition: hdmap_utils.cpp:879
auto getConflictingCrosswalkIds(const lanelet::Ids &) const -> lanelet::Ids
Definition: hdmap_utils.cpp:260
auto getNearbyLaneletIds(const geometry_msgs::msg::Point &, const double distance_threshold, const bool include_crosswalk, const std::size_t search_count=5) const -> lanelet::Ids
Definition: hdmap_utils.cpp:166
auto insertMarkerArray(visualization_msgs::msg::MarkerArray &, const visualization_msgs::msg::MarkerArray &) const -> void
Definition: hdmap_utils.cpp:926
auto countLaneChanges(const traffic_simulator_msgs::msg::LaneletPose &from, const traffic_simulator_msgs::msg::LaneletPose &to, const traffic_simulator::RoutingConfiguration &routing_configuration=traffic_simulator::RoutingConfiguration()) const -> std::optional< std::pair< int, int >>
Definition: hdmap_utils.cpp:72
Definition: cache.hpp:109
Definition: cache.hpp:30
Definition: sim_model_delay_steer_acc_geared_wo_fall_guard.hpp:26
Definition: cache.hpp:28
LaneletType
Definition: hdmap_utils.hpp:61
Definition: bounding_box.hpp:32
auto distance(const geometry_msgs::Pose &pose1, const geometry_msgs::Pose &pose2)
Definition: detection_sensor.cpp:45
Definition: lanelet_wrapper.hpp:40
TrajectoryShape
Definition: lane_change.hpp:31
Direction
Definition: lane_change.hpp:29
auto route(const lanelet::Id from_lanelet_id, const lanelet::Id to_lanelet_id, const RoutingConfiguration &routing_configuration=RoutingConfiguration()) -> lanelet::Ids
Definition: route.cpp:33
geometry_msgs::msg::Pose Pose
Definition: lanelet_wrapper.hpp:66
geometry_msgs::msg::Point Point
Definition: lanelet_wrapper.hpp:65
geometry_msgs::msg::Vector3 Vector3
Definition: lanelet_wrapper.hpp:70
RoutingGraphType
Definition: routing_graph_type.hpp:24
traffic_simulator_msgs::msg::LaneletPose LaneletPose
Definition: lanelet_pose.hpp:23
std::string string
Definition: junit5.hpp:26
lanelet::traffic_rules::TrafficRulesPtr rules
Definition: hdmap_utils.hpp:241
RouteCache route_cache
Definition: hdmap_utils.hpp:243
lanelet::routing::RoutingGraphPtr graph
Definition: hdmap_utils.hpp:242
Definition: routing_configuration.hpp:24
parameters for behavior plugin
Definition: lane_change.hpp:75