Apollo  6.0
Open source self driving car software
path.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of 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,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #pragma once
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 #include "modules/map/proto/map_lane.pb.h"
24 
25 #include "cyber/common/log.h"
32 
33 namespace apollo {
34 namespace hdmap {
35 
36 // class LaneInfoConstPtr;
37 // class OverlapInfoConstPtr;
38 
39 struct LaneWaypoint {
40  LaneWaypoint() = default;
42  : lane(CHECK_NOTNULL(lane)), s(s) {}
43  LaneWaypoint(LaneInfoConstPtr lane, const double s, const double l)
44  : lane(CHECK_NOTNULL(lane)), s(s), l(l) {}
46  double s = 0.0;
47  double l = 0.0;
48 
49  std::string DebugString() const;
50 };
51 
55 LaneBoundaryType::Type LeftBoundaryType(const LaneWaypoint& waypoint);
56 
60 LaneBoundaryType::Type RightBoundaryType(const LaneWaypoint& waypoint);
61 
67 
73 
74 struct LaneSegment {
75  LaneSegment() = default;
76  LaneSegment(LaneInfoConstPtr lane, const double start_s, const double end_s)
77  : lane(CHECK_NOTNULL(lane)), start_s(start_s), end_s(end_s) {}
79  double start_s = 0.0;
80  double end_s = 0.0;
81  double Length() const { return end_s - start_s; }
82 
86  static void Join(std::vector<LaneSegment>* segments);
87 
88  std::string DebugString() const;
89 };
90 
91 struct PathOverlap {
92  PathOverlap() = default;
93  PathOverlap(std::string object_id, const double start_s, const double end_s)
94  : object_id(std::move(object_id)), start_s(start_s), end_s(end_s) {}
95 
96  std::string object_id;
97  double start_s = 0.0;
98  double end_s = 0.0;
99 
100  std::string DebugString() const;
101 };
102 
104  public:
105  MapPathPoint() = default;
106  MapPathPoint(const common::math::Vec2d& point, double heading)
107  : Vec2d(point.x(), point.y()), heading_(heading) {}
108  MapPathPoint(const common::math::Vec2d& point, double heading,
109  LaneWaypoint lane_waypoint)
110  : Vec2d(point.x(), point.y()), heading_(heading) {
111  lane_waypoints_.emplace_back(std::move(lane_waypoint));
112  }
113  MapPathPoint(const common::math::Vec2d& point, double heading,
114  std::vector<LaneWaypoint> lane_waypoints)
115  : Vec2d(point.x(), point.y()),
116  heading_(heading),
117  lane_waypoints_(std::move(lane_waypoints)) {}
118 
119  double heading() const { return heading_; }
120  void set_heading(const double heading) { heading_ = heading; }
121 
122  const std::vector<LaneWaypoint>& lane_waypoints() const {
123  return lane_waypoints_;
124  }
125 
126  void add_lane_waypoint(LaneWaypoint lane_waypoint) {
127  lane_waypoints_.emplace_back(std::move(lane_waypoint));
128  }
129  void add_lane_waypoints(const std::vector<LaneWaypoint>& lane_waypoints) {
130  lane_waypoints_.insert(lane_waypoints_.end(), lane_waypoints.begin(),
131  lane_waypoints.end());
132  }
133 
134  void clear_lane_waypoints() { lane_waypoints_.clear(); }
135 
136  static void RemoveDuplicates(std::vector<MapPathPoint>* points);
137 
138  static std::vector<MapPathPoint> GetPointsFromSegment(
139  const LaneSegment& segment);
140 
141  static std::vector<MapPathPoint> GetPointsFromLane(LaneInfoConstPtr lane,
142  const double start_s,
143  const double end_s);
144 
145  std::string DebugString() const;
146 
147  protected:
148  double heading_ = 0.0;
149  std::vector<LaneWaypoint> lane_waypoints_;
150 };
151 
152 class Path;
153 
155  public:
156  PathApproximation() = default;
157  PathApproximation(const Path& path, const double max_error)
158  : max_error_(max_error), max_sqr_error_(max_error * max_error) {
159  Init(path);
160  }
161  double max_error() const { return max_error_; }
162  const std::vector<int>& original_ids() const { return original_ids_; }
163  const std::vector<common::math::LineSegment2d>& segments() const {
164  return segments_;
165  }
166 
167  bool GetProjection(const Path& path, const common::math::Vec2d& point,
168  double* accumulate_s, double* lateral,
169  double* distance) const;
170 
171  bool OverlapWith(const Path& path, const common::math::Box2d& box,
172  double width) const;
173 
174  protected:
175  void Init(const Path& path);
176  bool is_within_max_error(const Path& path, const int s, const int t);
177  double compute_max_error(const Path& path, const int s, const int t);
178 
179  void InitDilute(const Path& path);
180  void InitProjections(const Path& path);
181 
182  protected:
183  double max_error_ = 0;
184  double max_sqr_error_ = 0;
185 
186  int num_points_ = 0;
187  std::vector<int> original_ids_;
188  std::vector<common::math::LineSegment2d> segments_;
189  std::vector<double> max_error_per_segment_;
190 
191  // TODO(All): use direction change checks to early stop.
192 
193  // Projection of points onto the diluated segments.
194  std::vector<double> projections_;
196  int num_projection_samples_ = 0;
197 
198  // The original_projection is the projection of original points onto the
199  // diluated segments.
200  std::vector<double> original_projections_;
201  // max_p_to_left[i] = max(p[0], p[1], ... p[i]).
202  // min_p_to_right[i] = min(p[i], p[i + 1], ... p[size - 1]).
206 };
207 
209  public:
210  InterpolatedIndex(int id, double offset) : id(id), offset(offset) {}
211  int id = 0;
212  double offset = 0.0;
213 };
214 
215 class Path {
216  public:
217  Path() = default;
218  explicit Path(const std::vector<MapPathPoint>& path_points);
219  explicit Path(std::vector<MapPathPoint>&& path_points);
220  explicit Path(std::vector<LaneSegment>&& path_points);
221  explicit Path(const std::vector<LaneSegment>& path_points);
222 
223  Path(const std::vector<MapPathPoint>& path_points,
224  const std::vector<LaneSegment>& lane_segments);
225  Path(std::vector<MapPathPoint>&& path_points,
226  std::vector<LaneSegment>&& lane_segments);
227 
228  Path(const std::vector<MapPathPoint>& path_points,
229  const std::vector<LaneSegment>& lane_segments,
230  const double max_approximation_error);
231  Path(std::vector<MapPathPoint>&& path_points,
232  std::vector<LaneSegment>&& lane_segments,
233  const double max_approximation_error);
234 
235  // Return smooth coordinate by interpolated index or accumulate_s.
236  MapPathPoint GetSmoothPoint(const InterpolatedIndex& index) const;
237  MapPathPoint GetSmoothPoint(double s) const;
238 
239  // Compute accumulate s value of the index.
240  double GetSFromIndex(const InterpolatedIndex& index) const;
241  // Compute interpolated index by accumulate_s.
242  InterpolatedIndex GetIndexFromS(double s) const;
243 
244  // get the index of the lane from s by accumulate_s
245  InterpolatedIndex GetLaneIndexFromS(double s) const;
246 
247  std::vector<hdmap::LaneSegment> GetLaneSegments(const double start_s,
248  const double end_s) const;
249 
250  bool GetNearestPoint(const common::math::Vec2d& point, double* accumulate_s,
251  double* lateral) const;
252  bool GetNearestPoint(const common::math::Vec2d& point, double* accumulate_s,
253  double* lateral, double* distance) const;
254  bool GetProjectionWithHueristicParams(const common::math::Vec2d& point,
255  const double hueristic_start_s,
256  const double hueristic_end_s,
257  double* accumulate_s, double* lateral,
258  double* min_distance) const;
259  bool GetProjection(const common::math::Vec2d& point, double* accumulate_s,
260  double* lateral) const;
261  bool GetProjection(const common::math::Vec2d& point, double* accumulate_s,
262  double* lateral, double* distance) const;
263 
264  bool GetHeadingAlongPath(const common::math::Vec2d& point,
265  double* heading) const;
266 
267  int num_points() const { return num_points_; }
268  int num_segments() const { return num_segments_; }
269  const std::vector<MapPathPoint>& path_points() const { return path_points_; }
270  const std::vector<LaneSegment>& lane_segments() const {
271  return lane_segments_;
272  }
273  const std::vector<LaneSegment>& lane_segments_to_next_point() const {
274  return lane_segments_to_next_point_;
275  }
276  const std::vector<common::math::Vec2d>& unit_directions() const {
277  return unit_directions_;
278  }
279  const std::vector<double>& accumulated_s() const { return accumulated_s_; }
280  const std::vector<common::math::LineSegment2d>& segments() const {
281  return segments_;
282  }
283  const PathApproximation* approximation() const { return &approximation_; }
284  double length() const { return length_; }
285 
286  const PathOverlap* NextLaneOverlap(double s) const;
287 
288  const std::vector<PathOverlap>& lane_overlaps() const {
289  return lane_overlaps_;
290  }
291  const std::vector<PathOverlap>& signal_overlaps() const {
292  return signal_overlaps_;
293  }
294  const std::vector<PathOverlap>& yield_sign_overlaps() const {
295  return yield_sign_overlaps_;
296  }
297  const std::vector<PathOverlap>& stop_sign_overlaps() const {
298  return stop_sign_overlaps_;
299  }
300  const std::vector<PathOverlap>& crosswalk_overlaps() const {
301  return crosswalk_overlaps_;
302  }
303  const std::vector<PathOverlap>& junction_overlaps() const {
304  return junction_overlaps_;
305  }
306  const std::vector<PathOverlap>& pnc_junction_overlaps() const {
307  return pnc_junction_overlaps_;
308  }
309  const std::vector<PathOverlap>& clear_area_overlaps() const {
310  return clear_area_overlaps_;
311  }
312  const std::vector<PathOverlap>& speed_bump_overlaps() const {
313  return speed_bump_overlaps_;
314  }
315  const std::vector<PathOverlap>& parking_space_overlaps() const {
316  return parking_space_overlaps_;
317  }
318 
319  double GetLaneLeftWidth(const double s) const;
320  double GetLaneRightWidth(const double s) const;
321  bool GetLaneWidth(const double s, double* lane_left_width,
322  double* lane_right_width) const;
323 
324  double GetRoadLeftWidth(const double s) const;
325  double GetRoadRightWidth(const double s) const;
326  bool GetRoadWidth(const double s, double* road_left_width,
327  double* road_ight_width) const;
328 
329  bool IsOnPath(const common::math::Vec2d& point) const;
330  bool OverlapWith(const common::math::Box2d& box, double width) const;
331 
332  std::string DebugString() const;
333 
334  protected:
335  void Init();
336  void InitPoints();
337  void InitLaneSegments();
338  void InitWidth();
339  void InitPointIndex();
340  void InitOverlaps();
341 
342  double GetSample(const std::vector<double>& samples, const double s) const;
343 
344  using GetOverlapFromLaneFunc =
345  std::function<const std::vector<OverlapInfoConstPtr>&(const LaneInfo&)>;
346  void GetAllOverlaps(GetOverlapFromLaneFunc GetOverlaps_from_lane,
347  std::vector<PathOverlap>* const overlaps) const;
348 
349  protected:
350  int num_points_ = 0;
351  int num_segments_ = 0;
352  std::vector<MapPathPoint> path_points_;
353  std::vector<LaneSegment> lane_segments_;
354  std::vector<double> lane_accumulated_s_;
355  std::vector<LaneSegment> lane_segments_to_next_point_;
356  std::vector<common::math::Vec2d> unit_directions_;
357  double length_ = 0.0;
358  std::vector<double> accumulated_s_;
359  std::vector<common::math::LineSegment2d> segments_;
360  bool use_path_approximation_ = false;
362 
363  // Sampled every fixed length.
364  int num_sample_points_ = 0;
365  std::vector<double> lane_left_width_;
366  std::vector<double> lane_right_width_;
367  std::vector<double> road_left_width_;
368  std::vector<double> road_right_width_;
369  std::vector<int> last_point_index_;
370 
371  std::vector<PathOverlap> lane_overlaps_;
372  std::vector<PathOverlap> signal_overlaps_;
373  std::vector<PathOverlap> yield_sign_overlaps_;
374  std::vector<PathOverlap> stop_sign_overlaps_;
375  std::vector<PathOverlap> crosswalk_overlaps_;
376  std::vector<PathOverlap> parking_space_overlaps_;
377  std::vector<PathOverlap> junction_overlaps_;
378  std::vector<PathOverlap> pnc_junction_overlaps_;
379  std::vector<PathOverlap> clear_area_overlaps_;
380  std::vector<PathOverlap> speed_bump_overlaps_;
381 };
382 
383 } // namespace hdmap
384 } // namespace apollo
void clear_lane_waypoints()
Definition: path.h:134
std::vector< double > road_right_width_
Definition: path.h:368
Definition: path.h:91
std::vector< int > sampled_max_original_projections_to_left_
Definition: path.h:205
const std::vector< common::math::Vec2d > & unit_directions() const
Definition: path.h:276
double max_error() const
Definition: path.h:161
const std::vector< LaneSegment > & lane_segments_to_next_point() const
Definition: path.h:273
const std::vector< common::math::LineSegment2d > & segments() const
Definition: path.h:163
LaneBoundaryType::Type LeftBoundaryType(const LaneWaypoint &waypoint)
get left boundary type at a waypoint.
std::vector< common::math::LineSegment2d > segments_
Definition: path.h:359
const std::vector< PathOverlap > & yield_sign_overlaps() const
Definition: path.h:294
const std::vector< PathOverlap > & lane_overlaps() const
Definition: path.h:288
Define the LineSegment2d class.
std::vector< PathOverlap > junction_overlaps_
Definition: path.h:377
std::vector< LaneWaypoint > lane_waypoints_
Definition: path.h:149
void add_lane_waypoint(LaneWaypoint lane_waypoint)
Definition: path.h:126
std::vector< common::math::LineSegment2d > segments_
Definition: path.h:188
Defines the Vec2d class.
std::vector< double > road_left_width_
Definition: path.h:367
InterpolatedIndex(int id, double offset)
Definition: path.h:210
std::vector< double > max_error_per_segment_
Definition: path.h:189
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
const std::vector< PathOverlap > & pnc_junction_overlaps() const
Definition: path.h:306
MapPathPoint(const common::math::Vec2d &point, double heading)
Definition: path.h:106
std::vector< common::math::Vec2d > unit_directions_
Definition: path.h:356
double s
Definition: path.h:46
Definition: future.h:29
std::vector< PathOverlap > parking_space_overlaps_
Definition: path.h:376
void set_heading(const double heading)
Definition: path.h:120
std::vector< int > original_ids_
Definition: path.h:187
double heading() const
Definition: path.h:119
std::string object_id
Definition: path.h:96
std::vector< PathOverlap > speed_bump_overlaps_
Definition: path.h:380
std::vector< PathOverlap > crosswalk_overlaps_
Definition: path.h:375
Definition: path.h:154
Definition: path.h:74
LaneWaypoint LeftNeighborWaypoint(const LaneWaypoint &waypoint)
get left neighbor lane waypoint. If not exist, the Waypoint.lane will be null.
double l
Definition: path.h:47
Definition: path.h:103
const PathApproximation * approximation() const
Definition: path.h:283
std::vector< LaneSegment > lane_segments_
Definition: path.h:353
const std::vector< PathOverlap > & clear_area_overlaps() const
Definition: path.h:309
MapPathPoint(const common::math::Vec2d &point, double heading, LaneWaypoint lane_waypoint)
Definition: path.h:108
Rectangular (undirected) bounding box in 2-D.
Definition: box2d.h:52
Definition: path.h:208
std::vector< PathOverlap > clear_area_overlaps_
Definition: path.h:379
const std::vector< PathOverlap > & stop_sign_overlaps() const
Definition: path.h:297
int num_segments() const
Definition: path.h:268
Implements a class of 2-dimensional vectors.
Definition: vec2d.h:42
Definition: path.h:215
double max_projection_
Definition: path.h:195
const std::vector< LaneWaypoint > & lane_waypoints() const
Definition: path.h:122
std::vector< PathOverlap > signal_overlaps_
Definition: path.h:372
LaneWaypoint RightNeighborWaypoint(const LaneWaypoint &waypoint)
get left neighbor lane waypoint. If not exist, the Waypoint.lane will be null.
std::vector< PathOverlap > stop_sign_overlaps_
Definition: path.h:374
std::shared_ptr< const LaneInfo > LaneInfoConstPtr
Definition: hdmap_common.h:125
PathApproximation(const Path &path, const double max_error)
Definition: path.h:157
std::vector< PathOverlap > yield_sign_overlaps_
Definition: path.h:373
bool Init(const char *binary_name)
std::string DebugString() const
PathApproximation approximation_
Definition: path.h:361
std::vector< double > min_original_projections_to_right_
Definition: path.h:204
void add_lane_waypoints(const std::vector< LaneWaypoint > &lane_waypoints)
Definition: path.h:129
std::function< const std::vector< OverlapInfoConstPtr > &(const LaneInfo &)> GetOverlapFromLaneFunc
Definition: path.h:345
const std::vector< PathOverlap > & parking_space_overlaps() const
Definition: path.h:315
const std::vector< common::math::LineSegment2d > & segments() const
Definition: path.h:280
std::vector< double > original_projections_
Definition: path.h:200
std::vector< double > accumulated_s_
Definition: path.h:358
const std::vector< double > & accumulated_s() const
Definition: path.h:279
std::vector< double > projections_
Definition: path.h:194
Definition: hdmap_common.h:141
LaneSegment(LaneInfoConstPtr lane, const double start_s, const double end_s)
Definition: path.h:76
const std::vector< PathOverlap > & crosswalk_overlaps() const
Definition: path.h:300
double length() const
Definition: path.h:284
const std::vector< LaneSegment > & lane_segments() const
Definition: path.h:270
std::vector< double > lane_right_width_
Definition: path.h:366
const std::vector< PathOverlap > & speed_bump_overlaps() const
Definition: path.h:312
PathOverlap(std::string object_id, const double start_s, const double end_s)
Definition: path.h:93
std::vector< PathOverlap > pnc_junction_overlaps_
Definition: path.h:378
const std::vector< MapPathPoint > & path_points() const
Definition: path.h:269
std::vector< double > max_original_projections_to_left_
Definition: path.h:203
The class of Box2d. Here, the x/y axes are respectively Forward/Left, as opposed to what happens in e...
std::vector< double > lane_left_width_
Definition: path.h:365
std::vector< MapPathPoint > path_points_
Definition: path.h:352
std::vector< int > last_point_index_
Definition: path.h:369
std::vector< double > lane_accumulated_s_
Definition: path.h:354
const std::vector< PathOverlap > & signal_overlaps() const
Definition: path.h:291
const std::vector< int > & original_ids() const
Definition: path.h:162
std::vector< PathOverlap > lane_overlaps_
Definition: path.h:371
LaneBoundaryType::Type RightBoundaryType(const LaneWaypoint &waypoint)
get left boundary type at a waypoint.
double Length() const
Definition: path.h:81
std::vector< LaneSegment > lane_segments_to_next_point_
Definition: path.h:355
LaneWaypoint(LaneInfoConstPtr lane, const double s)
Definition: path.h:41
int num_points() const
Definition: path.h:267
LaneInfoConstPtr lane
Definition: path.h:45
LaneWaypoint(LaneInfoConstPtr lane, const double s, const double l)
Definition: path.h:43
const std::vector< PathOverlap > & junction_overlaps() const
Definition: path.h:303
MapPathPoint(const common::math::Vec2d &point, double heading, std::vector< LaneWaypoint > lane_waypoints)
Definition: path.h:113
Definition: path.h:39