Apollo  6.0
Open source self driving car software
st_boundary.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 
21 #pragma once
22 
23 #include <limits>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "gtest/gtest_prod.h"
33 #include "modules/planning/proto/planning.pb.h"
34 
35 namespace apollo {
36 namespace planning {
37 
39  public:
44  STBoundary() = default;
45  explicit STBoundary(
46  const std::vector<std::pair<STPoint, STPoint>>& point_pairs,
47  bool is_accurate_boundary = false);
48  explicit STBoundary(const common::math::Box2d& box) = delete;
49  explicit STBoundary(std::vector<common::math::Vec2d> points) = delete;
50 
53  static STBoundary CreateInstance(const std::vector<STPoint>& lower_points,
54  const std::vector<STPoint>& upper_points);
55 
60  const std::vector<STPoint>& lower_points,
61  const std::vector<STPoint>& upper_points);
62 
65  ~STBoundary() = default;
66 
67  bool IsEmpty() const { return lower_points_.empty(); }
68 
69  bool GetUnblockSRange(const double curr_time, double* s_upper,
70  double* s_lower) const;
71 
72  bool GetBoundarySRange(const double curr_time, double* s_upper,
73  double* s_lower) const;
74 
75  bool GetBoundarySlopes(const double curr_time, double* ds_upper,
76  double* ds_lower) const;
77 
78  // if you need to add boundary type, make sure you modify
79  // GetUnblockSRange accordingly.
80  enum class BoundaryType {
81  UNKNOWN,
82  STOP,
83  FOLLOW,
84  YIELD,
85  OVERTAKE,
86  KEEP_CLEAR,
87  };
88 
89  static std::string TypeName(BoundaryType type);
90 
92  const std::string& id() const;
93  double characteristic_length() const;
94 
95  void set_id(const std::string& id);
96  void SetBoundaryType(const BoundaryType& boundary_type);
97  void SetCharacteristicLength(const double characteristic_length);
98 
99  double min_s() const;
100  double min_t() const;
101  double max_s() const;
102  double max_t() const;
103 
104  std::vector<STPoint> upper_points() const { return upper_points_; }
105  std::vector<STPoint> lower_points() const { return lower_points_; }
106 
107  // Used by st-optimizer.
108  bool IsPointInBoundary(const STPoint& st_point) const;
109  STBoundary ExpandByS(const double s) const;
110  STBoundary ExpandByT(const double t) const;
111 
112  // Unused function so far.
113  STBoundary CutOffByT(const double t) const;
114 
115  // Used by Lattice planners.
116  STPoint upper_left_point() const;
117  STPoint upper_right_point() const;
118  STPoint bottom_left_point() const;
119  STPoint bottom_right_point() const;
120 
121  void set_upper_left_point(STPoint st_point);
122  void set_upper_right_point(STPoint st_point);
123  void set_bottom_left_point(STPoint st_point);
124  void set_bottom_right_point(STPoint st_point);
125 
126  void set_obstacle_road_right_ending_t(double road_right_ending_t) {
127  obstacle_road_right_ending_t_ = road_right_ending_t;
128  }
130  return obstacle_road_right_ending_t_;
131  }
132 
133  private:
136  bool IsValid(
137  const std::vector<std::pair<STPoint, STPoint>>& point_pairs) const;
138 
141  bool IsPointNear(const common::math::LineSegment2d& seg,
142  const common::math::Vec2d& point, const double max_dist);
143 
148  // TODO(all): When slope is high, this may introduce significant errors.
149  // Also, when accumulated for multiple t, the error can get significant.
150  // This function should be reconsidered, because it may be dangerous.
151  void RemoveRedundantPoints(
152  std::vector<std::pair<STPoint, STPoint>>* point_pairs);
153  FRIEND_TEST(StBoundaryTest, remove_redundant_points);
154 
159  bool GetIndexRange(const std::vector<STPoint>& points, const double t,
160  size_t* left, size_t* right) const;
161  FRIEND_TEST(StBoundaryTest, get_index_range);
162 
163  private:
164  BoundaryType boundary_type_ = BoundaryType::UNKNOWN;
165 
166  std::vector<STPoint> upper_points_;
167  std::vector<STPoint> lower_points_;
168 
169  std::string id_;
170  double characteristic_length_ = 1.0;
171  double min_s_ = std::numeric_limits<double>::max();
172  double max_s_ = std::numeric_limits<double>::lowest();
173  double min_t_ = std::numeric_limits<double>::max();
174  double max_t_ = std::numeric_limits<double>::lowest();
175 
176  STPoint bottom_left_point_;
177  STPoint bottom_right_point_;
178  STPoint upper_left_point_;
179  STPoint upper_right_point_;
180 
181  double obstacle_road_right_ending_t_;
182 };
183 
184 } // namespace planning
185 } // namespace apollo
const std::string & id() const
void set_bottom_left_point(STPoint st_point)
STBoundary ExpandByT(const double t) const
static STBoundary CreateInstanceAccurate(const std::vector< STPoint > &lower_points, const std::vector< STPoint > &upper_points)
Wrapper of the constructor. It doesn&#39;t use RemoveRedundantPoints and generates an accurate ST-boundar...
void SetCharacteristicLength(const double characteristic_length)
std::vector< STPoint > lower_points() const
Definition: st_boundary.h:105
STPoint upper_right_point() const
void set_obstacle_road_right_ending_t(double road_right_ending_t)
Definition: st_boundary.h:126
Defines the Vec2d class.
bool IsEmpty() const
Definition: st_boundary.h:67
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void set_upper_right_point(STPoint st_point)
Planning module main class. It processes GPS and IMU as input, to generate planning info...
The class of polygon in 2-D.
Definition: polygon2d.h:43
void set_bottom_right_point(STPoint st_point)
bool GetBoundarySRange(const double curr_time, double *s_upper, double *s_lower) const
BoundaryType boundary_type() const
void SetBoundaryType(const BoundaryType &boundary_type)
Rectangular (undirected) bounding box in 2-D.
Definition: box2d.h:52
bool GetUnblockSRange(const double curr_time, double *s_upper, double *s_lower) const
Definition: st_boundary.h:38
double characteristic_length() const
Line segment in 2-D.
Definition: line_segment2d.h:40
Definition: st_point.h:30
Implements a class of 2-dimensional vectors.
Definition: vec2d.h:42
std::vector< STPoint > upper_points() const
Definition: st_boundary.h:104
bool IsPointInBoundary(const STPoint &st_point) const
STBoundary CutOffByT(const double t) const
~STBoundary()=default
Default destructor.
The class of Box2d. Here, the x/y axes are respectively Forward/Left, as opposed to what happens in e...
STPoint upper_left_point() const
STPoint bottom_right_point() const
void set_id(const std::string &id)
void set_upper_left_point(STPoint st_point)
Define the Polygon2d class.
STPoint bottom_left_point() const
BoundaryType
Definition: st_boundary.h:80
static STBoundary CreateInstance(const std::vector< STPoint > &lower_points, const std::vector< STPoint > &upper_points)
Wrapper of the constructor (old).
STBoundary ExpandByS(const double s) const
static std::string TypeName(BoundaryType type)
double obstacle_road_right_ending_t() const
Definition: st_boundary.h:129
const std::vector< Vec2d > & points() const
Get the vertices of the polygon.
Definition: polygon2d.h:66
bool GetBoundarySlopes(const double curr_time, double *ds_upper, double *ds_lower) const