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