Apollo  v5.5.0
Open source self driving car software
projection_cache.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 <map>
19 #include <string>
20 #include <vector>
21 
22 #include "Eigen/StdVector"
24 
26 
27 namespace apollo {
28 namespace perception {
29 namespace fusion {
30 
31 // @brief: project cache of object
33  public:
34  ProjectionCacheObject() : start_ind_(0), end_ind_(0) {
35  box_ = base::BBox2DF();
36  }
37  explicit ProjectionCacheObject(size_t point2ds_size)
38  : start_ind_(point2ds_size), end_ind_(point2ds_size) {
39  box_ = base::BBox2DF();
40  }
41  // getters
42  size_t GetStartInd() const { return start_ind_; }
43  size_t GetEndInd() const { return end_ind_; }
44  base::BBox2DF GetBox() const { return box_; }
45  // setters
46  void SetStartInd(size_t ind) { start_ind_ = ind; }
47  void SetEndInd(size_t ind) { end_ind_ = ind; }
48  void SetBox(base::BBox2DF box) { box_ = box; }
49  const bool Empty() const { return (start_ind_ == end_ind_); }
50  size_t Size() const { return (end_ind_ - start_ind_); }
51 
52  private:
53  // project pts cache index of start/end, the pts of CacheObject belongs
54  // to [start_ind_, end_ind_) of point2ds of Cache
55  size_t start_ind_;
56  size_t end_ind_;
57  base::BBox2DF box_;
58 }; // class ProjectionCacheObject
59 
60 // @brief: project cache of frame
62  public:
63  ProjectionCacheFrame() : sensor_id_(""), timestamp_(0.0) {}
64  ProjectionCacheFrame(std::string sensor_id, double timestamp)
65  : sensor_id_(sensor_id), timestamp_(timestamp) {}
66  bool VerifyKey(std::string sensor_id, double timestamp) {
67  if (sensor_id_ != sensor_id || fabs(timestamp_ - timestamp) > DBL_EPSILON) {
68  return false;
69  }
70  return true;
71  }
72  ProjectionCacheObject* BuildObject(int lidar_object_id) {
73  objects_[lidar_object_id] = ProjectionCacheObject();
74  return QueryObject(lidar_object_id);
75  }
76  ProjectionCacheObject* QueryObject(int lidar_object_id) {
77  auto it = objects_.find(lidar_object_id);
78  if (it == objects_.end()) {
79  return nullptr;
80  } else {
81  return &(it->second);
82  }
83  }
84 
85  private:
86  // sensor id of cached project frame
87  std::string sensor_id_;
88  double timestamp_;
89  std::map<int, ProjectionCacheObject> objects_;
90 }; // class ProjectionCacheFrame
91 
92 // @brief: project cache
94  public:
95  ProjectionCache() : measurement_sensor_id_(""), measurement_timestamp_(0.0) {
96  // 300,000 pts is 2 times of the size of point cloud of ordinary frame of
97  // velodyne64
98  point2ds_.reserve(300000);
99  }
100  ProjectionCache(std::string sensor_id, double timestamp)
101  : measurement_sensor_id_(sensor_id), measurement_timestamp_(timestamp) {
102  // 300,000 pts is 2 times of the size of point cloud of ordinary frame of
103  // velodyne64
104  point2ds_.reserve(300000);
105  }
106  // reset projection cache
107  void Reset(std::string sensor_id, double timestamp) {
108  measurement_sensor_id_ = sensor_id;
109  measurement_timestamp_ = timestamp;
110  point2ds_.clear();
111  frames_.clear();
112  }
113  // getters
114  Eigen::Vector2d* GetPoint2d(size_t ind) {
115  if (ind >= point2ds_.size()) {
116  return nullptr;
117  }
118  return &(point2ds_[ind]);
119  }
120  size_t GetPoint2dsSize() const { return point2ds_.size(); }
121  // add point
122  void AddPoint(const Eigen::Vector2f& pt) {
123  point2ds_.emplace_back(pt.x(), pt.y());
124  }
125  // add object
126  ProjectionCacheObject* BuildObject(const std::string& measurement_sensor_id,
127  double measurement_timestamp,
128  const std::string& projection_sensor_id,
129  double projection_timestamp,
130  int lidar_object_id) {
131  if (!VerifyKey(measurement_sensor_id, measurement_timestamp)) {
132  return nullptr;
133  }
134  ProjectionCacheFrame* frame =
135  QueryFrame(projection_sensor_id, projection_timestamp);
136  if (frame == nullptr) {
137  frame = BuildFrame(projection_sensor_id, projection_timestamp);
138  }
139  if (frame == nullptr) {
140  return nullptr;
141  }
142  return frame->BuildObject(lidar_object_id);
143  }
144  // query projection cache object
145  ProjectionCacheObject* QueryObject(const std::string& measurement_sensor_id,
146  double measurement_timestamp,
147  const std::string& projection_sensor_id,
148  double projection_timestamp,
149  int lidar_object_id) {
150  if (!VerifyKey(measurement_sensor_id, measurement_timestamp)) {
151  return nullptr;
152  }
153  ProjectionCacheFrame* frame =
154  QueryFrame(projection_sensor_id, projection_timestamp);
155  if (frame == nullptr) {
156  return nullptr;
157  }
158  return frame->QueryObject(lidar_object_id);
159  }
160 
161  private:
162  bool VerifyKey(const std::string& sensor_id, double timestamp) {
163  if (measurement_sensor_id_ != sensor_id ||
164  fabs(measurement_timestamp_ - timestamp) > DBL_EPSILON) {
165  return false;
166  }
167  return true;
168  }
169  ProjectionCacheFrame* BuildFrame(const std::string& sensor_id,
170  double timestamp) {
171  frames_.push_back(ProjectionCacheFrame(sensor_id, timestamp));
172  return &(frames_[frames_.size() - 1]);
173  }
174  ProjectionCacheFrame* QueryFrame(const std::string& sensor_id,
175  double timestamp) {
176  for (size_t i = 0; i < frames_.size(); ++i) {
177  if (!frames_[i].VerifyKey(sensor_id, timestamp)) {
178  continue;
179  }
180  return &(frames_[i]);
181  }
182  return nullptr;
183  }
184 
185  private:
186  // sensor id & timestamp of measurement, which are the key of project cache
187  std::string measurement_sensor_id_;
188  double measurement_timestamp_;
189  // project cache memeory
190  std::vector<Eigen::Vector2d> point2ds_;
191  // cache reference on frames
192  std::vector<ProjectionCacheFrame> frames_;
193 }; // class ProjectionCache
194 
196 
197 } // namespace fusion
198 } // namespace perception
199 } // namespace apollo
ProjectionCacheObject * QueryObject(const std::string &measurement_sensor_id, double measurement_timestamp, const std::string &projection_sensor_id, double projection_timestamp, int lidar_object_id)
Definition: projection_cache.h:145
void Reset(std::string sensor_id, double timestamp)
Definition: projection_cache.h:107
ProjectionCacheObject * BuildObject(const std::string &measurement_sensor_id, double measurement_timestamp, const std::string &projection_sensor_id, double projection_timestamp, int lidar_object_id)
Definition: projection_cache.h:126
bool VerifyKey(std::string sensor_id, double timestamp)
Definition: projection_cache.h:66
ProjectionCache()
Definition: projection_cache.h:95
size_t Size() const
Definition: projection_cache.h:50
Definition: blob.h:72
BBox2D< float > BBox2DF
Definition: box.h:164
Definition: projection_cache.h:32
ProjectionCache * ProjectionCachePtr
Definition: projection_cache.h:195
ProjectionCacheFrame(std::string sensor_id, double timestamp)
Definition: projection_cache.h:64
EIGEN_DEFINE_STL_VECTOR_SPECIALIZATION(Eigen::Vector2d)
Definition: projection_cache.h:93
ProjectionCacheObject * QueryObject(int lidar_object_id)
Definition: projection_cache.h:76
ProjectionCache(std::string sensor_id, double timestamp)
Definition: projection_cache.h:100
void AddPoint(const Eigen::Vector2f &pt)
Definition: projection_cache.h:122
void SetEndInd(size_t ind)
Definition: projection_cache.h:47
void SetBox(base::BBox2DF box)
Definition: projection_cache.h:48
base::BBox2DF GetBox() const
Definition: projection_cache.h:44
ProjectionCacheFrame()
Definition: projection_cache.h:63
void SetStartInd(size_t ind)
Definition: projection_cache.h:46
ProjectionCacheObject()
Definition: projection_cache.h:34
const bool Empty() const
Definition: projection_cache.h:49
size_t GetStartInd() const
Definition: projection_cache.h:42
size_t GetPoint2dsSize() const
Definition: projection_cache.h:120
Definition: projection_cache.h:61
ProjectionCacheObject * BuildObject(int lidar_object_id)
Definition: projection_cache.h:72
size_t GetEndInd() const
Definition: projection_cache.h:43
Eigen::Vector2d * GetPoint2d(size_t ind)
Definition: projection_cache.h:114
ProjectionCacheObject(size_t point2ds_size)
Definition: projection_cache.h:37