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