Apollo  6.0
Open source self driving car software
frame_list.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "cyber/common/log.h"
26 
27 namespace apollo {
28 namespace perception {
29 namespace camera {
30 
31 struct alignas(16) PatchIndicator {
32  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34  frame_id = patch_id = -1;
35  sensor_name = "";
36  }
37 
39  this->frame_id = frame_id;
40  this->patch_id = patch_id;
41  sensor_name = "";
42  }
43  PatchIndicator(int frame_id, int patch_id, const std::string &sensor_name) {
44  this->frame_id = frame_id;
45  this->patch_id = patch_id;
46  this->sensor_name = sensor_name;
47  }
48  bool operator==(const PatchIndicator &indicator) {
49  return (frame_id == indicator.frame_id && patch_id == indicator.patch_id);
50  }
51 
52  std::string to_string() const {
53  std::stringstream str;
54  str << sensor_name << " | " << frame_id << " (" << patch_id << ")";
55  return str.str();
56  }
57 
58  int frame_id;
59  int patch_id;
60  std::string sensor_name;
61 };
62 
63 struct alignas(16) SimilarMap {
64  public:
65  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
66  bool Init(int dim, int gpu_id = 0, int init_size = 128) {
67  if (dim == 0) {
68  return false;
69  }
70 
72 
73  this->dim = dim;
74  map_sim.resize(dim);
75  for (int i = 0; i < dim; ++i) {
76  map_sim[i].resize(dim);
77  for (int j = 0; j < dim; ++j) {
78  map_sim[i][j].reset(new base::Blob<float>);
79  map_sim[i][j]->Reshape({init_size, init_size}); // 最大跟踪128个目标, m_max=n_max=128
80  // allocate CPU/GPU memory
81  map_sim[i][j]->cpu_data();
82  map_sim[i][j]->gpu_data();
83  }
84  }
85  return true;
86  }
87 
88  void set(int frame1, int frame2, std::shared_ptr<base::Blob<float>> sim) {
89  map_sim[frame1 % dim][frame2 % dim] = sim;
90  }
91 
92  const std::shared_ptr<base::Blob<float>> get(int frame1, int frame2) const {
93  return map_sim[frame1 % dim][frame2 % dim];
94  }
95 
96  float sim(const PatchIndicator &p1, const PatchIndicator &p2) const {
97  auto blob = get(p1.frame_id, p2.frame_id); // 根据frame_ids定位14*14矩阵的位置
98  return *(blob->cpu_data() + blob->offset(p1.patch_id, p2.patch_id)); // 通过patch_ids进一步定位到m*n矩阵的位置,得到objs的相似度
99  }
100 
101  std::vector<std::vector<std::shared_ptr<base::Blob<float>>>> map_sim;
102  int dim;
103 };
104 
105 class FrameList {
106  public:
107  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
108  FrameList() { Init(1); }
109 
110  bool Init(int cap) {
111  if (cap <= 0) {
112  return false;
113  }
114  capability_ = cap;
115  frame_count_ = 0;
116  frames_.resize(capability_);
117  return true;
118  }
119 
120  inline int OldestFrameId() {
121  if (frame_count_ < capability_) {
122  return 0;
123  } else {
124  return get_frame(frame_count_)->frame_id;
125  }
126  }
127 
128  inline int Size() {
129  if (frame_count_ < capability_) {
130  return frame_count_;
131  } else {
132  return capability_;
133  }
134  }
135 
136  inline void Add(CameraFrame *frame) {
137  frames_[frame_count_ % capability_] = frame;
138  ++frame_count_;
139  }
140 
141  inline CameraFrame *get_frame(int index) const {
142  assert(index <= frame_count_);
143  if (index < 0) {
144  return frames_[(index + frame_count_) % capability_];
145  } else {
146  return frames_[index % capability_];
147  }
148  }
149 
150  inline CameraFrame *operator[](int index) const { return get_frame(index); }
151 
152  inline base::ObjectPtr get_object(PatchIndicator indicator) const {
153  return get_frame(indicator.frame_id)->detected_objects[indicator.patch_id];
154  }
155 
156  private:
157  int frame_count_ = 0;
158  int capability_ = 0;
159  std::vector<CameraFrame *> frames_;
160 };
161 
162 } // namespace camera
163 } // namespace perception
164 } // namespace apollo
std::string to_string() const
Definition: frame_list.h:52
static bool set_device_id(int device_id)
bool Init(int cap)
Definition: frame_list.h:110
Definition: camera_frame.h:33
CameraFrame * operator[](int index) const
Definition: frame_list.h:150
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void Add(CameraFrame *frame)
Definition: frame_list.h:136
EIGEN_MAKE_ALIGNED_OPERATOR_NEW FrameList()
Definition: frame_list.h:108
int Size()
Definition: frame_list.h:128
int dim
Definition: frame_list.h:102
std::vector< std::vector< std::shared_ptr< base::Blob< float > > > > map_sim
Definition: frame_list.h:101
CameraFrame * get_frame(int index) const
Definition: frame_list.h:141
EIGEN_MAKE_ALIGNED_OPERATOR_NEW bool Init(int dim, int gpu_id=0, int init_size=128)
Definition: frame_list.h:66
EIGEN_MAKE_ALIGNED_OPERATOR_NEW PatchIndicator()
Definition: frame_list.h:33
float sim(const PatchIndicator &p1, const PatchIndicator &p2) const
Definition: frame_list.h:96
Definition: frame_list.h:63
PatchIndicator(int frame_id, int patch_id, const std::string &sensor_name)
Definition: frame_list.h:43
bool operator==(const PatchIndicator &indicator)
Definition: frame_list.h:48
base::ObjectPtr get_object(PatchIndicator indicator) const
Definition: frame_list.h:152
bool Init(const char *binary_name)
int frame_id
Definition: frame_list.h:58
int patch_id
Definition: frame_list.h:59
PatchIndicator(int frame_id, int patch_id)
Definition: frame_list.h:38
Definition: frame_list.h:105
int OldestFrameId()
Definition: frame_list.h:120
std::shared_ptr< Object > ObjectPtr
Definition: object.h:123
std::string sensor_name
Definition: frame_list.h:60