Apollo 11.0
自动驾驶开放平台
fusion.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2020 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
18
19#include <cmath>
20#include <utility>
21
22namespace apollo {
23namespace v2x {
24namespace ft {
25
27 ft_config_manager_ptr_ = FTConfigManager::Instance();
28
29 score_params_ = ft_config_manager_ptr_->fusion_params_.params.score_params();
30 switch (score_params_.confidence_level()) {
32 m_matched_dis_limit_ = std::sqrt(4.605);
33 break;
35 m_matched_dis_limit_ = std::sqrt(5.991);
36 break;
38 m_matched_dis_limit_ = std::sqrt(7.378);
39 break;
41 m_matched_dis_limit_ = std::sqrt(9.210);
42 break;
43 default:
44 break;
45 }
46}
47
49 last_timestamp_ = -1.0;
50 return true;
51}
52
54 const std::vector<std::vector<base::Object>> &input_objectlists,
55 double timestamp) {
56 fusion_result_.clear();
57 updated_objects_.clear();
58 for (unsigned int i = 0; i < input_objectlists.size(); ++i) {
59 CombineNewResource(input_objectlists[i]);
60 }
61 return true;
62}
63
64bool Fusion::CombineNewResource(const std::vector<base::Object> &new_objects) {
65 return CombineNewResource(new_objects, &updated_objects_, &fusion_result_);
66}
67
69 const std::vector<base::Object> &new_objects,
70 std::vector<base::Object> *fused_objects,
71 std::vector<std::vector<base::Object>> *fusion_result) {
72 if (new_objects.empty()) {
73 return false;
74 }
75 if (fused_objects->size() < 1) {
76 fused_objects->assign(new_objects.begin(), new_objects.end());
77 for (unsigned int j = 0; j < new_objects.size(); ++j) {
78 std::vector<base::Object> matched_objects;
79 matched_objects.push_back(new_objects[j]);
80 fusion_result->push_back(matched_objects);
81 }
82 return true;
83 }
84 int u_num = fused_objects->size();
85 int v_num = new_objects.size();
86 Eigen::MatrixXf association_mat(u_num, v_num);
87 ComputeAssociateMatrix(*fused_objects, new_objects, &association_mat);
88 std::vector<std::pair<int, int>> match_cps;
89 if (u_num > v_num) {
90 km_matcher_.GetKMResult(association_mat.transpose(), &match_cps, true);
91 } else {
92 km_matcher_.GetKMResult(association_mat, &match_cps, false);
93 }
94 for (auto it = match_cps.begin(); it != match_cps.end(); it++) {
95 if (it->second != -1) {
96 if (it->first == -1) {
97 fused_objects->push_back(new_objects[it->second]);
98 std::vector<base::Object> matched_objects;
99 matched_objects.push_back(fused_objects->back());
100 fusion_result->push_back(matched_objects);
101 } else {
102 (*fusion_result)[it->first].push_back(new_objects[it->second]);
103 }
104 }
105 }
106 return true;
107}
108
110 const std::vector<std::vector<base::Object>> &fusion_result,
111 std::vector<base::Object> *fused_objects) {
112 for (const auto &objects : fusion_result) {
113 if (objects.size() == 1) {
114 fused_objects->push_back(objects.at(0));
115 if (objects.at(0).frame_id == "V2X") {
116 fused_objects->back().v2x_type = base::V2xType::BLIND_ZONE;
117 } else {
118 fused_objects->back().v2x_type = base::V2xType::UNKNOWN;
119 }
120 } else {
121 fused_objects->push_back(objects.at(0));
122 host_vehicle_ = false;
123 zom_vehicle_ = false;
124 for (const auto &object : objects) {
125 if (object.v2x_type == base::V2xType::HOST_VEHICLE) {
126 host_vehicle_ = true;
127 } else if (object.v2x_type == base::V2xType::ZOMBIES_CAR) {
128 zom_vehicle_ = true;
129 }
130 }
131 if (zom_vehicle_ == true) {
132 fused_objects->back().v2x_type = base::V2xType::ZOMBIES_CAR;
133 }
134 if (host_vehicle_ == true) {
135 fused_objects->back().v2x_type = base::V2xType::HOST_VEHICLE;
136 }
137 }
138 }
139 return true;
140}
141
142double Fusion::CheckOdistance(const base::Object &in1_ptr,
143 const base::Object &in2_ptr) {
144 double xi = in1_ptr.position.x();
145 double yi = in1_ptr.position.y();
146 double xj = in2_ptr.position.x();
147 double yj = in2_ptr.position.y();
148 double distance = std::hypot(xi - xj, yi - yj);
149 return distance;
150}
151
152bool Fusion::CheckDisScore(const base::Object &in1_ptr,
153 const base::Object &in2_ptr, double *score) {
154 double dis = CheckOdistance(in1_ptr, in2_ptr);
155 *score = 2.5 * std::max(0.0, score_params_.max_match_distance() - dis);
156 return true;
157}
158
159bool Fusion::CheckTypeScore(const base::Object &in1_ptr,
160 const base::Object &in2_ptr, double *score) {
161 double same_prob = 0;
162 if (in1_ptr.sub_type == in2_ptr.sub_type) {
163 same_prob =
164 1 - (1 - in1_ptr.sub_type_probs[0]) * (1 - in2_ptr.sub_type_probs[0]);
165 } else if (in1_ptr.type == in2_ptr.type ||
166 in1_ptr.type == v2x::ft::base::ObjectType::UNKNOWN ||
167 in2_ptr.type == v2x::ft::base::ObjectType::UNKNOWN) {
168 same_prob =
169 (1 - in1_ptr.sub_type_probs.at(0)) * in2_ptr.sub_type_probs.at(0) +
170 (1 - in2_ptr.sub_type_probs.at(0)) * in1_ptr.sub_type_probs.at(0);
171 same_prob *= score_params_.prob_scale();
172 }
173 *score *= same_prob;
174 return true;
175}
176
177bool Fusion::ComputeAssociateMatrix(
178 const std::vector<base::Object> &in1_objects, // fused
179 const std::vector<base::Object> &in2_objects, // new
180 Eigen::MatrixXf *association_mat) {
181 for (unsigned int i = 0; i < in1_objects.size(); ++i) {
182 for (unsigned int j = 0; j < in2_objects.size(); ++j) {
183 const base::Object &obj1_ptr = in1_objects[i];
184 const base::Object &obj2_ptr = in2_objects[j];
185 double score = 0;
186 if (!CheckDisScore(obj1_ptr, obj2_ptr, &score)) {
187 AERROR << "V2X Fusion: check dis score failed";
188 }
189 if (score_params_.check_type() &&
190 !CheckTypeScore(obj1_ptr, obj2_ptr, &score)) {
191 AERROR << "V2X Fusion: check type failed";
192 }
193 (*association_mat)(i, j) =
194 (score >= score_params_.min_score()) ? score : 0;
195 }
196 }
197 return true;
198}
199
200int Fusion::DeleteRedundant(std::vector<base::Object> *objects) {
201 std::vector<unsigned int> to_be_deleted;
202 for (unsigned int i = 0; i < objects->size(); ++i) {
203 for (unsigned int j = i + 1; j < objects->size(); ++j) {
204 double distance = CheckOdistance(objects->at(i), objects->at(j));
205 if (distance < 1) {
206 to_be_deleted.push_back(j);
207 }
208 }
209 }
210 for (auto iter = to_be_deleted.rbegin(); iter != to_be_deleted.rend();
211 ++iter) {
212 objects->erase(objects->begin() + *iter);
213 }
214 return to_be_deleted.size();
215}
216} // namespace ft
217} // namespace v2x
218} // namespace apollo
bool CombineNewResource(const std::vector< base::Object > &new_objects, std::vector< base::Object > *fused_objects, std::vector< std::vector< base::Object > > *fusion_result)
Definition fusion.cc:68
int DeleteRedundant(std::vector< base::Object > *objects)
Definition fusion.cc:200
bool GetV2xFusionObjects(const std::vector< std::vector< base::Object > > &fusion_result, std::vector< base::Object > *fused_objects)
Definition fusion.cc:109
bool Proc(const std::vector< std::vector< base::Object > > &input_objectlists, double timestamp)
Definition fusion.cc:53
bool GetKMResult(const T &association_mat, std::vector< std::pair< int, int > > *match_cps, bool need_reverse=false)
Definition km.h:60
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37
optional ConfidenceLevel confidence_level