Apollo  v5.5.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 
33  frame_id = patch_id = -1;
34  sensor_name = "";
35  }
36 
38  this->frame_id = frame_id;
39  this->patch_id = patch_id;
40  sensor_name = "";
41  }
42  PatchIndicator(int frame_id, int patch_id, const std::string &sensor_name) {
43  this->frame_id = frame_id;
44  this->patch_id = patch_id;
45  this->sensor_name = sensor_name;
46  }
47  bool operator==(const PatchIndicator &indicator) {
48  return (frame_id == indicator.frame_id && patch_id == indicator.patch_id);
49  }
50 
51  std::string to_string() const {
52  std::stringstream str;
53  str << sensor_name << " | " << frame_id << " (" << patch_id << ")";
54  return str.str();
55  }
56 
57  int frame_id;
58  int patch_id;
59  std::string sensor_name;
60 };
61 
62 struct SimilarMap {
63  public:
64  bool Init(int dim, int gpu_id = 0, int init_size = 128) {
65  if (dim == 0) {
66  return false;
67  }
68 
70 
71  this->dim = dim;
72  map_sim.resize(dim);
73  for (int i = 0; i < dim; ++i) {
74  map_sim[i].resize(dim);
75  for (int j = 0; j < dim; ++j) {
76  map_sim[i][j].reset(new base::Blob<float>);
77  map_sim[i][j]->Reshape({init_size, init_size});
78  // allocate CPU/GPU memory
79  map_sim[i][j]->cpu_data();
80  map_sim[i][j]->gpu_data();
81  }
82  }
83  return true;
84  }
85 
86  void set(int frame1, int frame2, std::shared_ptr<base::Blob<float>> sim) {
87  map_sim[frame1 % dim][frame2 % dim] = sim;
88  }
89 
90  const std::shared_ptr<base::Blob<float>> get(int frame1, int frame2) const {
91  return map_sim[frame1 % dim][frame2 % dim];
92  }
93 
94  float sim(const PatchIndicator &p1, const PatchIndicator &p2) const {
95  auto blob = get(p1.frame_id, p2.frame_id);
96  return *(blob->cpu_data() + blob->offset(p1.patch_id, p2.patch_id));
97  }
98 
99  std::vector<std::vector<std::shared_ptr<base::Blob<float>>>> map_sim;
100  int dim;
101 };
102 
103 class FrameList {
104  public:
105  FrameList() { Init(1); }
106 
107  bool Init(int cap) {
108  if (cap <= 0) {
109  return false;
110  }
111  capability_ = cap;
112  frame_count_ = 0;
113  frames_.resize(capability_);
114  return true;
115  }
116 
117  inline int OldestFrameId() {
118  if (frame_count_ < capability_) {
119  return 0;
120  } else {
121  return get_frame(frame_count_)->frame_id;
122  }
123  }
124 
125  inline int Size() {
126  if (frame_count_ < capability_) {
127  return frame_count_;
128  } else {
129  return capability_;
130  }
131  }
132 
133  inline void Add(CameraFrame *frame) {
134  frames_[frame_count_ % capability_] = frame;
135  ++frame_count_;
136  }
137 
138  inline CameraFrame *get_frame(int index) const {
139  assert(index <= frame_count_);
140  if (index < 0) {
141  return frames_[(index + frame_count_) % capability_];
142  } else {
143  return frames_[index % capability_];
144  }
145  }
146 
147  inline CameraFrame *operator[](int index) const { return get_frame(index); }
148 
149  inline base::ObjectPtr get_object(PatchIndicator indicator) const {
150  return get_frame(indicator.frame_id)->detected_objects[indicator.patch_id];
151  }
152 
153  private:
154  int frame_count_ = 0;
155  int capability_ = 0;
156  std::vector<CameraFrame *> frames_;
157 };
158 
159 } // namespace camera
160 } // namespace perception
161 } // namespace apollo
std::string to_string() const
Definition: frame_list.h:51
static bool set_device_id(int device_id)
bool Init(int cap)
Definition: frame_list.h:107
Definition: camera_frame.h:33
CameraFrame * operator[](int index) const
Definition: frame_list.h:147
Definition: blob.h:72
void Add(CameraFrame *frame)
Definition: frame_list.h:133
int Size()
Definition: frame_list.h:125
int dim
Definition: frame_list.h:100
PatchIndicator()
Definition: frame_list.h:32
std::vector< std::vector< std::shared_ptr< base::Blob< float > > > > map_sim
Definition: frame_list.h:99
CameraFrame * get_frame(int index) const
Definition: frame_list.h:138
float sim(const PatchIndicator &p1, const PatchIndicator &p2) const
Definition: frame_list.h:94
Definition: frame_list.h:62
PatchIndicator(int frame_id, int patch_id, const std::string &sensor_name)
Definition: frame_list.h:42
bool operator==(const PatchIndicator &indicator)
Definition: frame_list.h:47
base::ObjectPtr get_object(PatchIndicator indicator) const
Definition: frame_list.h:149
int frame_id
Definition: frame_list.h:57
int patch_id
Definition: frame_list.h:58
PatchIndicator(int frame_id, int patch_id)
Definition: frame_list.h:37
bool Init(int dim, int gpu_id=0, int init_size=128)
Definition: frame_list.h:64
Definition: frame_list.h:103
int OldestFrameId()
Definition: frame_list.h:117
FrameList()
Definition: frame_list.h:105
std::shared_ptr< Object > ObjectPtr
Definition: object.h:118
std::string sensor_name
Definition: frame_list.h:59