Apollo  v5.5.0
Open source self driving car software
cipv_camera.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2019 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 <array>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "Eigen/Dense"
27 #include "Eigen/Eigen"
28 #include "Eigen/Geometry"
29 
30 #include "modules/common/configs/vehicle_config_helper.h"
34 
35 namespace apollo {
36 namespace perception {
37 
38 struct CipvOptions {
39  float velocity = 5.0f;
40  float yaw_rate = 0.0f;
41  float yaw_angle = 0.0f;
42 };
43 
44 constexpr float kMinVelocity = 10.0f; // in m/s
45 constexpr float kMaxDistObjectToLaneInMeter = 70.0f;
46 constexpr float kMaxDistObjectToVirtualLaneInMeter = 10.0f;
47 constexpr float kMaxDistObjectToLaneInPixel = 10.0f;
48 const std::size_t kDropsHistorySize = 20;
49 const std::size_t kMaxObjectNum = 100;
50 const std::size_t kMaxAllowedSkipObject = 10;
51 
52 static constexpr uint32_t kMaxNumVirtualLanePoint = 25;
53 // TODO(All) average image frame rate should come from other header file.
54 static constexpr float kAverageFrameRate = 0.05f;
55 
56 class Cipv {
57  // Member functions
58  public:
59  Cipv(void);
60  virtual ~Cipv(void);
61 
62  virtual bool Init(
63  const Eigen::Matrix3d &homography_im2car,
64  const float min_laneline_length_for_cipv = kMinLaneLineLengthForCIPV,
65  const float average_lane_width_in_meter = kAverageLaneWidthInMeter,
66  const float max_vehicle_width_in_meter = kMaxVehicleWidthInMeter,
67  const float average_frame_rate = kAverageFrameRate,
68  const bool image_based_cipv = false, const int debug_level = 0);
69  virtual std::string Name() const;
70 
71  // Determine CIPV among multiple objects
72  bool DetermineCipv(const std::vector<base::LaneLine> &lane_objects,
73  const CipvOptions &options,
74  const Eigen::Affine3d &world2camera,
75  std::vector<std::shared_ptr<base::Object>> *objects);
76 
77  // Collect drops for tailgating
78  bool CollectDrops(const base::MotionBufferPtr &motion_buffer,
79  const Eigen::Affine3d &world2camera,
80  std::vector<std::shared_ptr<base::Object>> *objects);
81 
82  static float VehicleDynamics(const uint32_t tick, const float yaw_rate,
83  const float velocity, const float time_unit,
84  float *x, float *y);
85  static float VehicleDynamics(const uint32_t tick, const float yaw_rate,
86  const float velocity, const float time_unit,
87  const float vehicle_half_width, float *center_x,
88  float *ceneter_y, float *left_x, float *left_y,
89  float *right_x, float *right_y);
90  // Make a virtual lane line using a yaw_rate
91  static bool MakeVirtualEgoLaneFromYawRate(const float yaw_rate,
92  const float velocity,
93  const float offset_distance,
94  LaneLineSimple *left_lane_line,
95  LaneLineSimple *right_lane_line);
96 
97  private:
98  // Distance from a point to a line segment
99  bool DistanceFromPointToLineSegment(const Point2Df &point,
100  const Point2Df &line_seg_start_point,
101  const Point2Df &line_seg_end_point,
102  float *distance);
103 
104  // Determine CIPV among multiple objects
105  bool GetEgoLane(const std::vector<base::LaneLine> &lane_objects,
106  EgoLane *egolane_image, EgoLane *egolane_ground,
107  bool *b_left_valid, bool *b_right_valid);
108 
109  // Elongate lane line
110  bool ElongateEgoLane(const std::vector<base::LaneLine> &lane_objects,
111  const bool b_left_valid, const bool b_right_valid,
112  const float yaw_rate, const float velocity,
113  EgoLane *egolane_image, EgoLane *egolane_ground);
114  bool CreateVirtualEgoLane(const float yaw_rate, const float velocity,
115  EgoLane *egolane_ground);
116 
117  // Get closest edge of an object in image coordinate
118  bool FindClosestObjectImage(const std::shared_ptr<base::Object> &object,
119  const EgoLane &egolane_image,
120  LineSegment2Df *closted_object_edge,
121  float *distance);
122 
123  // Get closest edge of an object in ground coordinate
124  bool FindClosestObjectGround(const std::shared_ptr<base::Object> &object,
125  const EgoLane &egolane_ground,
126  const Eigen::Affine3d world2camera,
127  LineSegment2Df *closted_object_edge,
128  float *distance);
129 
130  // Check if the distance between lane and object are OK
131  bool AreDistancesSane(const float distance_start_point_to_right_lane,
132  const float distance_start_point_to_left_lane,
133  const float distance_end_point_to_right_lane,
134  const float distance_end_point_to_left_lane);
135 
136  // Check if the object is in the lane in image space
137  bool IsObjectInTheLaneImage(const std::shared_ptr<base::Object> &object,
138  const EgoLane &egolane_image, float *distance);
139  // Check if the object is in the lane in ego-ground space
140  // | |
141  // | *------* |
142  // | *-+-----*
143  // | | *--------* <- closest edge of object
144  // *+------* |
145  // | |
146  // l_lane r_lane
147  bool IsObjectInTheLaneGround(const std::shared_ptr<base::Object> &object,
148  const EgoLane &egolane_ground,
149  const Eigen::Affine3d world2camera,
150  const bool b_virtual, float *distance);
151 
152  // Check if the object is in the lane in ego-ground space
153  bool IsObjectInTheLane(const std::shared_ptr<base::Object> &object,
154  const EgoLane &egolane_image,
155  const EgoLane &egolane_ground,
156  const Eigen::Affine3d world2camera,
157  const bool b_virtual, float *distance);
158 
159  // Check if a point is left of a line segment
160  bool IsPointLeftOfLine(const Point2Df &point,
161  const Point2Df &line_seg_start_point,
162  const Point2Df &line_seg_end_point);
163 
164  // Make a virtual lane line using a reference lane line and its offset
165  // distance
166  bool MakeVirtualLane(const LaneLineSimple &ref_lane_line,
167  const float yaw_rate, const float offset_distance,
168  LaneLineSimple *virtual_lane_line);
169 
170  // transform point to another using motion
171  bool TranformPoint(const Eigen::VectorXf &in,
172  const Eigen::Matrix4f &motion_matrix,
173  Eigen::Vector3d *out);
174 
175  bool image2ground(const float image_x, const float image_y, float *ground_x,
176  float *ground_y);
177  bool ground2image(const float ground_x, const float ground_y, float *image_x,
178  float *image_y);
179 
180  // Member variables
181  bool b_image_based_cipv_ = false;
182  int32_t debug_level_ = 0;
183  float time_unit_ = kAverageFrameRate;
184 
185  float min_laneline_length_for_cipv_ = kMinLaneLineLengthForCIPV;
186  float average_lane_width_in_meter_ = kAverageLaneWidthInMeter;
187  float max_vehicle_width_in_meter_ = kMaxVehicleWidthInMeter;
188  float margin_vehicle_to_lane_ =
190  float single_virtual_egolane_width_in_meter_ =
192  float half_virtual_egolane_width_in_meter_ =
193  single_virtual_egolane_width_in_meter_ * 0.5f;
194  float half_vehicle_width_in_meter_ = kMaxVehicleWidthInMeter * 0.5f;
195  float max_dist_object_to_lane_in_meter_ = kMaxDistObjectToLaneInMeter;
196  float max_dist_object_to_virtual_lane_in_meter_ =
198  float max_dist_object_to_lane_in_pixel_ = kMaxDistObjectToLaneInPixel;
199  float MAX_VEHICLE_WIDTH_METER = 5.0f;
200  float EPSILON = 1.0e-6f;
201  std::size_t kDropsHistorySize = 100;
202  std::size_t kMaxObjectNum = 100;
203  std::size_t kMaxAllowedSkipObject = 10;
204 
205  std::map<int, size_t> object_id_skip_count_;
206  std::map<int, boost::circular_buffer<std::pair<float, float>>>
207  object_trackjectories_;
208  std::map<int, std::vector<double>> object_timestamps_;
209  Eigen::Matrix3d homography_im2car_ = Eigen::Matrix3d::Identity();
210  Eigen::Matrix3d homography_car2im_ = Eigen::Matrix3d::Identity();
211  int32_t old_cipv_track_id_ = -2;
212 };
213 
214 } // namespace perception
215 } // namespace apollo
float yaw_angle
Definition: cipv_camera.h:41
Definition: lane_object.h:75
constexpr float kMaxDistObjectToVirtualLaneInMeter
Definition: cipv_camera.h:46
Definition: blob.h:72
float yaw_rate
Definition: cipv_camera.h:40
constexpr float kMaxDistObjectToLaneInPixel
Definition: cipv_camera.h:47
constexpr uint32_t kMinLaneLineLengthForCIPV
Definition: lane_object.h:35
constexpr float kMinVelocity
Definition: cipv_camera.h:44
float velocity
Definition: cipv_camera.h:39
Eigen::Vector2f Point2Df
Definition: lane_object.h:51
Definition: cipv_camera.h:56
constexpr float kAverageLaneWidthInMeter
Definition: lane_object.h:37
std::shared_ptr< MotionBuffer > MotionBufferPtr
Definition: object_supplement.h:201
constexpr float kMaxDistObjectToLaneInMeter
Definition: cipv_camera.h:45
Definition: lane_object.h:64
Definition: lane_object.h:55
const std::size_t kMaxObjectNum
Definition: cipv_camera.h:49
const std::size_t kMaxAllowedSkipObject
Definition: cipv_camera.h:50
constexpr float kMaxVehicleWidthInMeter
Definition: lane_object.h:39
constexpr float kMarginVehicleToLane
Definition: lane_object.h:41
const std::size_t kDropsHistorySize
Definition: cipv_camera.h:48
Definition: cipv_camera.h:38