Apollo 11.0
自动驾驶开放平台
object_sequence.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 <utility>
19
20#include "cyber/common/log.h"
21
22namespace apollo {
23namespace perception {
24namespace radar4d {
25
26using ObjectPtr = std::shared_ptr<apollo::perception::base::Object>;
27
29 const std::vector<ObjectPtr>& objects, TimeStampKey timestamp) {
30 std::lock_guard<std::mutex> lock(mutex_);
31 for (const auto& obj : objects) {
32 TrackIdKey& track_id = obj->track_id;
33 auto iter = sequence_.find(track_id);
34 if (iter == sequence_.end()) {
35 auto res = sequence_.insert(std::make_pair(track_id, TrackedObjects()));
36 iter = res.first;
37 }
38 auto res = iter->second.insert(std::make_pair(timestamp, obj));
39 if (!res.second) {
40 AERROR << "Fail to insert object." << std::endl;
41 return false;
42 }
43 }
44 RemoveStaleTracks(timestamp);
45 current_ = timestamp;
46 return true;
47}
48
50 TrackedObjects* track,
51 TimeStampKey window_time) {
52 if (track == nullptr) {
53 return false;
54 }
55 track->clear();
56 std::lock_guard<std::mutex> lock(mutex_);
57 double start_time = current_ - window_time;
58 auto iter = sequence_.find(track_id);
59 if (iter == sequence_.end()) {
60 return false;
61 }
62 for (auto& tobj : iter->second) {
63 if (tobj.first >= start_time) {
64 track->insert(tobj);
65 }
66 }
67 return true;
68}
69
71 for (auto outer_iter = sequence_.begin(); outer_iter != sequence_.end();) {
72 if (outer_iter->second.empty()) {
73 AERROR << "Found empty tracks";
74 continue;
75 }
76 auto& track = outer_iter->second;
77
78 if (current_stamp - track.rbegin()->first > kMaxTimeOut) {
79 sequence_.erase(outer_iter++);
80 continue;
81 }
82 for (auto inner_iter = track.begin(); inner_iter != track.end();) {
83 if (current_stamp - inner_iter->first > kMaxTimeOut) {
84 track.erase(inner_iter++);
85 continue;
86 } else {
87 break;
88 }
89 }
90 if (track.empty()) { // all element removed
91 sequence_.erase(outer_iter++);
92 } else {
93 ++outer_iter;
94 }
95 }
96}
97
98} // namespace radar4d
99} // namespace perception
100} // namespace apollo
bool AddTrackedFrameObjects(const std::vector< std::shared_ptr< perception::base::Object > > &objects, TimeStampKey timestamp)
static constexpr TimeStampKey kMaxTimeOut
std::map< TimeStampKey, std::shared_ptr< apollo::perception::base::Object > > TrackedObjects
bool GetTrackInTemporalWindow(TrackIdKey track_id, TrackedObjects *track, TimeStampKey window_time)
void RemoveStaleTracks(TimeStampKey current_stamp)
std::map< TrackIdKey, TrackedObjects > sequence_
#define AERROR
Definition log.h:44
std::shared_ptr< apollo::perception::base::Object > ObjectPtr
class register implement
Definition arena_queue.h:37