Apollo  6.0
Open source self driving car software
sequence_maintainer.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 #pragma once
17 
18 #include <iostream>
19 #include <list>
20 #include <map>
21 #include <vector>
23 
24 namespace apollo {
25 namespace perception {
26 namespace benchmark {
27 
28 template <typename ObjectKey>
29 using SequenceType = std::map<ObjectKey, ObjectPtr>;
30 
31 template <typename ObjectKey>
33  public:
34  SequenceMaintainer() = default;
35  ~SequenceMaintainer() = default;
36  bool add_data(const std::vector<ObjectPtr>& objects, ObjectKey key);
37  auto get_sequence(int sequence_id) -> SequenceType<ObjectKey>*;
38  void clear() { _sequence.clear(); }
39 
40  protected:
41  std::map<int, SequenceType<ObjectKey>> _sequence;
42 
43  private:
44  static const std::size_t _s_max_sample_num = 10;
45  static constexpr double _s_max_lift_time = 10.0;
46 };
47 
48 template <typename ObjectKey>
50  const std::vector<ObjectPtr>& objects, ObjectKey key) {
51  for (const auto& obj : objects) {
52  auto& id = obj->track_id;
53  if (_sequence.size() > 0 && _sequence.begin()->first > id) {
54  std::cerr << "Find track_id roll back, so clear the cache sequence, "
55  << "current id " << id << " oldest id "
56  << _sequence.begin()->first << "." << std::endl;
57  _sequence.clear();
58  }
59  auto& sub = _sequence[id];
60  if (sub.size() > 0 && sub.rbegin()->first >= key) {
61  // std::cerr << "New added key can not be less than old key, "
62  // << key << " to be added but " << sub.rbegin()->first << " exist."
63  // << std::endl;
64  return false;
65  }
66  sub[key] = obj;
67  auto iter = sub.begin();
68  while (sub.size() > _s_max_sample_num) {
69  sub.erase(iter++);
70  }
71  }
72  auto iter = _sequence.begin();
73  while (iter != _sequence.end()) {
74  if (iter->second.empty() ||
75  static_cast<double>(key - iter->second.rbegin()->first) >
76  _s_max_lift_time) {
77  _sequence.erase(iter++);
78  } else {
79  ++iter;
80  }
81  }
82  return true;
83 }
84 
85 template <typename ObjectKey>
88  auto iter = _sequence.find(sequence_id);
89  if (iter == _sequence.end()) {
90  return nullptr;
91  } else {
92  return &(iter->second);
93  }
94 }
95 
96 } // namespace benchmark
97 } // namespace perception
98 } // namespace apollo
void clear()
Definition: sequence_maintainer.h:38
std::map< int, SequenceType< ObjectKey > > _sequence
Definition: sequence_maintainer.h:41
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
auto get_sequence(int sequence_id) -> SequenceType< ObjectKey > *
Definition: sequence_maintainer.h:86
std::map< ObjectKey, ObjectPtr > SequenceType
Definition: sequence_maintainer.h:29
Definition: sequence_maintainer.h:32
bool add_data(const std::vector< ObjectPtr > &objects, ObjectKey key)
Definition: sequence_maintainer.h:49