Apollo 11.0
自动驾驶开放平台
distance_collection.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2023 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 *****************************************************************************/
17
18#include <algorithm>
19#include <vector>
20
21#include "cyber/common/log.h"
26
27namespace apollo {
28namespace perception {
29namespace radar4d {
30
31float LocationDistance(const TrackedObjectConstPtr& last_object,
32 const Eigen::VectorXf& track_predict,
33 const TrackedObjectConstPtr& new_object,
34 const double time_diff) {
35 // Compute locatin distance for last object and new object
36 // range from 0 to positive infinity
37
38 Eigen::Vector3f measured_anchor_point =
39 (new_object->anchor_point).cast<float>();
40 Eigen::Vector3f predicted_anchor_point = track_predict.head(3);
41 Eigen::Vector3f measure_predict_diff =
42 measured_anchor_point - predicted_anchor_point;
43
44 float location_dist = static_cast<float>(sqrt(
45 (measure_predict_diff.head(2).cwiseProduct(measure_predict_diff.head(2)))
46 .sum()));
47
48 /* NEED TO NOTICE: All the states would be collected mainly based on
49 * states of tracked objects. Thus, update tracked object when you
50 * update the state of track !!!!! */
51 Eigen::Vector2d ref_dir = last_object->output_velocity.head(2);
52 double speed = ref_dir.norm();
53 ref_dir /= speed;
54
55 /* Let location distance generated from a normal distribution with
56 * symmetric variance. Modify its variance when speed greater than
57 * a threshold. Penalize variance other than motion direction. */
58 if (speed > 2) {
59 Eigen::Vector2d ref_o_dir = Eigen::Vector2d(ref_dir[1], -ref_dir[0]);
60 double dx = ref_dir(0) * measure_predict_diff(0) +
61 ref_dir(1) * measure_predict_diff(1);
62 double dy = ref_o_dir(0) * measure_predict_diff(0) +
63 ref_o_dir(1) * measure_predict_diff(1);
64 location_dist = static_cast<float>(sqrt(dx * dx * 0.5 + dy * dy * 2));
65 }
66
67 return location_dist;
68}
69
71 const Eigen::VectorXf& track_predict,
72 const TrackedObjectConstPtr& new_object,
73 const double time_diff) {
74 // Compute direction distance for last object and new object
75 // range from 0 to 2
76
77 Eigen::Vector3f old_anchor_point =
78 (last_object->belief_anchor_point).cast<float>();
79 Eigen::Vector3f new_anchor_point = (new_object->anchor_point).cast<float>();
80 Eigen::Vector3f anchor_point_shift_dir = new_anchor_point - old_anchor_point;
81 anchor_point_shift_dir[2] = 0.0;
82
83 Eigen::Vector3f track_motion_dir = track_predict.head(6).tail(3);
84 track_motion_dir[2] = 0.0;
85
86 double cos_theta = 0.994; // average cos
87 if (!track_motion_dir.head(2).isZero() &&
88 !anchor_point_shift_dir.head(2).isZero()) {
89 // cos_theta = vector_cos_theta_2d_xy(track_motion_dir,
90 // anchor_point_shift_dir);
91 cos_theta = algorithm::CalculateCosTheta2DXY<float>(track_motion_dir,
92 anchor_point_shift_dir);
93 }
94 float direction_dist = static_cast<float>(-cos_theta) + 1.0f;
95
96 return direction_dist;
97}
98
99float BboxSizeDistance(const TrackedObjectConstPtr& last_object,
100 const Eigen::VectorXf& track_predict,
101 const TrackedObjectConstPtr& new_object,
102 const double time_diff) {
103 // Compute bbox size distance for last object and new object
104 // range from 0 to 1
105
106 Eigen::Vector3f old_bbox_dir = last_object->output_direction.cast<float>();
107 Eigen::Vector3f new_bbox_dir = new_object->direction.cast<float>();
108 Eigen::Vector3f old_bbox_size = last_object->output_size.cast<float>();
109 Eigen::Vector3f new_bbox_size = new_object->size.cast<float>();
110
111 float size_dist = 0.0f;
112 double dot_val_00 = fabs(old_bbox_dir(0) * new_bbox_dir(0) +
113 old_bbox_dir(1) * new_bbox_dir(1));
114 double dot_val_01 = fabs(old_bbox_dir(0) * new_bbox_dir(1) -
115 old_bbox_dir(1) * new_bbox_dir(0));
116 float temp_val_0 = 0.0f;
117 float temp_val_1 = 0.0f;
118 if (dot_val_00 > dot_val_01) {
119 temp_val_0 = static_cast<float>(fabs(old_bbox_size(0) - new_bbox_size(0))) /
120 std::max(old_bbox_size(0), new_bbox_size(0));
121 temp_val_1 = static_cast<float>(fabs(old_bbox_size(1) - new_bbox_size(1))) /
122 std::max(old_bbox_size(1), new_bbox_size(1));
123 size_dist = std::min(temp_val_0, temp_val_1);
124 } else {
125 temp_val_0 = static_cast<float>(fabs(old_bbox_size(0) - new_bbox_size(1))) /
126 std::max(old_bbox_size(0), new_bbox_size(1));
127 temp_val_1 = static_cast<float>(fabs(old_bbox_size(1) - new_bbox_size(0))) /
128 std::max(old_bbox_size(1), new_bbox_size(0));
129 size_dist = std::min(temp_val_0, temp_val_1);
130 }
131
132 return size_dist;
133}
134
136 const Eigen::VectorXf& track_predict,
137 const TrackedObjectConstPtr& new_object,
138 const double time_diff) {
139 // Compute point num distance for last object and new object
140 // range from 0 and 1
141
142 int old_point_number = static_cast<int>(
143 (last_object->object_ptr->radar4d_supplement).cloud_world.size());
144 int new_point_number = static_cast<int>(
145 (new_object->object_ptr->radar4d_supplement).cloud_world.size());
146
147 float point_num_dist =
148 static_cast<float>(fabs(old_point_number - new_point_number)) * 1.0f /
149 static_cast<float>(std::max(old_point_number, new_point_number));
150
151 return point_num_dist;
152}
153
155 const Eigen::VectorXf& track_predict,
156 const TrackedObjectConstPtr& new_object,
157 const double time_diff) {
158 // Compute histogram distance for last object and new object
159 // range from 0 to 3
160
161 const std::vector<float>& old_object_shape_features =
162 last_object->shape_features;
163 const std::vector<float>& new_object_shape_features =
164 new_object->shape_features;
165
166 if (old_object_shape_features.size() != new_object_shape_features.size()) {
167 AINFO << "sizes of compared features not matched. TrackObjectDistance";
168 return 100;
169 }
170
171 float histogram_dist = 0.0f;
172 for (size_t i = 0; i < old_object_shape_features.size(); ++i) {
173 histogram_dist +=
174 std::fabs(old_object_shape_features[i] - new_object_shape_features[i]);
175 }
176
177 return histogram_dist;
178}
179
181 const Eigen::VectorXf& track_predict,
182 const TrackedObjectConstPtr& new_object,
183 const double time_diff) {
184 float dist = static_cast<float>(
185 (last_object->barycenter.head(2) - new_object->barycenter.head(2))
186 .norm());
187 return dist;
188}
189
190float BboxIouDistance(const TrackedObjectConstPtr& last_object,
191 const Eigen::VectorXf& track_predict,
192 const TrackedObjectConstPtr& new_object,
193 const double time_diff, double match_threshold) {
194 // Step 1: unify bbox direction, change the one with less pts,
195 // for efficiency.
196 Eigen::Vector3f old_dir = last_object->output_direction.cast<float>();
197 Eigen::Vector3f old_size = last_object->output_size.cast<float>();
198 Eigen::Vector3d old_center = last_object->output_center;
199 Eigen::Vector3f new_dir = new_object->direction.cast<float>();
200 Eigen::Vector3f new_size = new_object->size.cast<float>();
201 Eigen::Vector3d new_center = new_object->center;
202 old_dir.normalize();
203 new_dir.normalize();
204 // handle randomness
205 old_size(0) = old_size(0) > 0.3f ? old_size(0) : 0.3f;
206 old_size(1) = old_size(1) > 0.3f ? old_size(1) : 0.3f;
207 new_size(0) = new_size(0) > 0.3f ? new_size(0) : 0.3f;
208 new_size(1) = new_size(1) > 0.3f ? new_size(1) : 0.3f;
209 int last_object_num_pts = static_cast<int>(
210 (last_object->object_ptr->radar4d_supplement).cloud_world.size());
211 int cur_obj_num_pts = static_cast<int>(
212 (new_object->object_ptr->radar4d_supplement).cloud_world.size());
213 bool change_cur_obj_bbox = last_object_num_pts > cur_obj_num_pts;
214 float minimum_edge_length = 0.01f;
216 (new_object->object_ptr->radar4d_supplement).cloud_world;
217 if (change_cur_obj_bbox) {
218 new_dir = old_dir;
220 cloud, new_dir, &new_size, &new_center, minimum_edge_length);
221 } else {
222 old_dir = new_dir;
224 cloud, old_dir, &old_size, &old_center, minimum_edge_length);
225 }
226 // Step 2: compute 2d iou bbox to bbox
227 Eigen::Matrix2d trans_mat;
228 trans_mat(0, 0) = (old_dir.cast<double>())(0);
229 trans_mat(0, 1) = (old_dir.cast<double>())(1);
230 trans_mat(1, 0) = -(old_dir.cast<double>())(1);
231 trans_mat(1, 1) = (old_dir.cast<double>())(0);
232 Eigen::Vector2d old_center_transformed_2d =
233 static_cast<Eigen::Matrix<double, 2, 1, 0, 2, 1>>(trans_mat *
234 old_center.head(2));
235 Eigen::Vector2d new_center_transformed_2d =
236 static_cast<Eigen::Matrix<double, 2, 1, 0, 2, 1>>(trans_mat *
237 new_center.head(2));
238 old_center(0) = old_center_transformed_2d(0);
239 old_center(1) = old_center_transformed_2d(1);
240 new_center(0) = new_center_transformed_2d(0);
241 new_center(1) = new_center_transformed_2d(1);
242 Eigen::Vector3d old_size_tmp = old_size.cast<double>();
243 Eigen::Vector3d new_size_tmp = new_size.cast<double>();
244 double iou = algorithm::CalculateIou2DXY<double>(old_center, old_size_tmp,
245 new_center, new_size_tmp);
246 // Step 4: compute dist
247 double dist = (1 - iou) * match_threshold;
248 return static_cast<float>(dist);
249}
250
251// TODO(all): semantic map related, for debugging
252// float SemanticMapDistance(const MrfTrackData& track_dat,
253// const TrackedObjectConstPtr& cur_obj) {
254// if (track_dat.feature_ &&
255// track_dat.feature_->predicted_trajectory_size() > 0) {
256// const apollo::prediction::Trajectory& traj =
257// track_dat.feature_->predicted_trajectory(0);
258// apollo::common::math::Vec2d pt(
259// cur_obj->center[0] - cur_obj->global_local_offset[0],
260// cur_obj->center[1] - cur_obj->global_local_offset[1]);
261// std::vector<float> dist_vec;
262// for (int i = 0; i < traj.trajectory_point_size() - 1; ++i) {
263// const apollo::common::TrajectoryPoint& pt_st = traj.trajectory_point(i);
264// const apollo::common::TrajectoryPoint& pt_et =
265// traj.trajectory_point(i + 1);
266// apollo::common::math::Vec2d start(pt_st.path_point().x(),
267// pt_st.path_point().y());
268// apollo::common::math::Vec2d end(pt_et.path_point().x(),
269// pt_et.path_point().y());
270// apollo::common::math::LineSegment2d seg(start, end);
271// double dist = seg.DistanceTo(pt);
272// dist_vec.push_back(static_cast<float>(dist));
273// }
274// float min_dist = *(std::min_element(dist_vec.begin(), dist_vec.end()));
275// ADEBUG << "min_dist calculated for track id " << track_dat.track_id_
276// << " object id " << cur_obj->track_id << " distance " << min_dist;
277// return min_dist;
278// }
279//
280// return 0.0f;
281//}
282
283} // namespace radar4d
284} // namespace perception
285} // namespace apollo
Define the LineSegment2d class.
#define AINFO
Definition log.h:42
void CalculateBBoxSizeCenter2DXY(const PointCloudT &cloud, const Eigen::Vector3f &dir, Eigen::Vector3f *size, Eigen::Vector3d *center, float minimum_edge_length=std::numeric_limits< float >::epsilon())
Definition common.h:117
AttributeRadarPointCloud< RadarPointD > RadarPointDCloud
float BboxSizeDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff)
Compute bbox size distance for given track & object
float PointNumDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff)
Compute point num distance for given track & object
std::shared_ptr< const TrackedObject > TrackedObjectConstPtr
float LocationDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff)
Compute location distance for given track & object
float CentroidShiftDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff)
Compute centroid shift distance for object and background match
float BboxIouDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff, double match_threshold)
Compute bbox iou distance for object and background match
float DirectionDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff)
Compute direction distance for given track & object
float HistogramDistance(const TrackedObjectConstPtr &last_object, const Eigen::VectorXf &track_predict, const TrackedObjectConstPtr &new_object, const double time_diff)
Compute histogram distance for given track & object
class register implement
Definition arena_queue.h:37
Defines the Vec2d class.