Apollo 10.0
自动驾驶开放平台
bitmap2d.cc
浏览该文件的文档.
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
18
20
21namespace apollo {
22namespace perception {
23namespace lidar {
24
25static constexpr uint64_t kZeroLast = static_cast<uint64_t>(-1) - 1;
26
27// static
29 const Bitmap2D::DirectionMajor dir_major) {
30 return static_cast<DirectionMajor>(static_cast<int>(dir_major) ^ 1);
31}
32
33inline bool Bitmap2D::CheckBit(const size_t loc, const uint64_t block) const {
34 return block & (static_cast<uint64_t>(1) << loc);
35}
36
37inline void Bitmap2D::SetBit(const size_t loc, uint64_t* block) {
38 (*block) |= (static_cast<uint64_t>(1) << loc);
39}
40
41inline void Bitmap2D::ResetBit(const size_t loc, uint64_t* block) {
42 (*block) &= (~(static_cast<uint64_t>(1) << loc));
43}
44
45inline void Bitmap2D::SetTailBits(const size_t tail_num, uint64_t* block) {
46 (*block) |= (~(static_cast<uint64_t>(-1) << tail_num));
47}
48
49inline void Bitmap2D::ResetTailBits(const size_t tail_num, uint64_t* block) {
50 (*block) &= (static_cast<uint64_t>(-1) << tail_num);
51}
52
53inline void Bitmap2D::SetHeadBits(const size_t tail_num, uint64_t* block) {
54 (*block) |= (static_cast<uint64_t>(-1) << tail_num);
55}
56
57inline void Bitmap2D::ResetHeadBits(const size_t tail_num, uint64_t* block) {
58 (*block) &= ~(static_cast<uint64_t>(-1) << tail_num);
59}
60
61inline void Bitmap2D::SetRangeBits(const size_t head, const size_t tail,
62 uint64_t* block) {
63 // note: it not work when head = 64
64 (*block) |= ((static_cast<uint64_t>(-1) << tail) & (~(kZeroLast << head)));
65}
66
67inline void Bitmap2D::ResetRangeBits(const size_t head, const size_t tail,
68 uint64_t* block) {
69 (*block) &= ((~(static_cast<uint64_t>(-1) << tail)) | (kZeroLast << head));
70}
71
72void Bitmap2D::Init(const Eigen::Vector2d& min_range,
73 const Eigen::Vector2d& max_range,
74 const Eigen::Vector2d& cell_size) {
75 CHECK_GT(cell_size.x(), 0);
76 CHECK_GT(cell_size.y(), 0);
77 CHECK_GT(max_range.x(), min_range.x() + cell_size.x());
78 CHECK_GT(max_range.y(), min_range.y() + cell_size.y());
79
80 min_range_ = min_range;
81 max_range_ = max_range;
82 cell_size_ = cell_size;
83
84 dims_ =
85 ((max_range_ - min_range_).array() / cell_size_.array()).cast<size_t>();
86 dims_ += Vec2ui(1, 1);
87 map_size_[0] = dims_[0];
88 map_size_[1] = (dims_[1] >> 6) + 1;
89 bitmap_.resize(map_size_[0] * map_size_[1], 0);
90}
91
92void Bitmap2D::SetUp(const DirectionMajor dir_major) {
93 dir_major_ = dir_major;
94 op_dir_major_ = OppositeDirection(dir_major);
95 memset(bitmap_.data(), 0, sizeof(bitmap_[0]) * bitmap_.size());
96}
97
98// point to bitmap index;
99inline Bitmap2D::Vec3ui Bitmap2D::RealToBitmap(const Eigen::Vector2d& p) const {
100 Vec2ui pix = ((p - min_range_).array() / cell_size_.array()).cast<size_t>();
101 Vec2ui major_pix(pix[dir_major()], pix[op_dir_major()]);
102 Vec3ui bit_p;
103 bit_p.x() = major_pix.x();
104 bit_p.y() = major_pix.y() >> 6;
105 bit_p.z() = major_pix.y() & 63;
106 return bit_p;
107}
108
109// calc index by bitmap index;
110inline int Bitmap2D::Index(const Bitmap2D::Vec3ui& p) const {
111 return static_cast<int>(p.x() * map_size_[1] + p.y());
112}
113
114// range check
115bool Bitmap2D::IsExists(const Eigen::Vector2d& p) const {
116 return p.x() >= min_range_.x() && p.x() < max_range_.x() &&
117 p.y() >= min_range_.y() && p.y() < max_range_.y();
118}
119
120// check
121bool Bitmap2D::Check(const Eigen::Vector2d& p) const {
122 const Vec3ui bit_p = RealToBitmap(p);
123 const int idx = Index(bit_p);
124 return CheckBit(bit_p.z(), bitmap_[idx]);
125}
126
127// set and reset
128void Bitmap2D::Set(const Eigen::Vector2d& p) {
129 const Vec3ui bit_p = RealToBitmap(p);
130 const int idx = Index(bit_p);
131 return SetBit(bit_p.z(), &bitmap_[idx]);
132}
133
134void Bitmap2D::Reset(const Eigen::Vector2d& p) {
135 const Vec3ui bit_p = RealToBitmap(p);
136 const int idx = Index(bit_p);
137 return ResetBit(bit_p.z(), &bitmap_[idx]);
138}
139
140void Bitmap2D::Set(const double x, const double min_y, const double max_y) {
141 Eigen::Vector2d real_left, real_right;
142 real_left[op_dir_major()] = min_y;
143 real_right[op_dir_major()] = max_y;
144 real_left[dir_major()] = real_right[dir_major()] = x;
145 const Vec3ui left_bit_p = RealToBitmap(real_left);
146 const Vec3ui right_bit_p = RealToBitmap(real_right);
147 if (left_bit_p.y() == right_bit_p.y()) {
148 const int idx = Index(left_bit_p);
149 SetRangeBits(right_bit_p.z(), left_bit_p.z(), &bitmap_[idx]);
150 return;
151 }
152 const size_t left_idx = Index(left_bit_p);
153 const size_t right_idx = Index(right_bit_p);
154 SetHeadBits(left_bit_p.z(), &bitmap_[left_idx]);
155 SetTailBits(right_bit_p.z(), &bitmap_[right_idx]);
156 for (size_t i = left_idx + 1; i < right_idx; ++i) {
157 bitmap_[i] = static_cast<uint64_t>(-1);
158 }
159}
160
161void Bitmap2D::Reset(const double x, const double min_y, const double max_y) {
162 Eigen::Vector2d real_left, real_right;
163 real_left[op_dir_major()] = min_y;
164 real_right[op_dir_major()] = max_y;
165 real_left[dir_major()] = real_right[dir_major()] = x;
166 const Vec3ui left_bit_p = RealToBitmap(real_left);
167 const Vec3ui right_bit_p = RealToBitmap(real_right);
168 if (left_bit_p.y() == right_bit_p.y()) {
169 const int idx = Index(left_bit_p);
170 ResetRangeBits(right_bit_p.z() + 1, left_bit_p.z(), &bitmap_[idx]);
171 return;
172 }
173 // set first block and last block
174 const size_t left_idx = Index(left_bit_p);
175 const size_t right_idx = Index(right_bit_p);
176 ResetHeadBits(left_bit_p.z(), &bitmap_[left_idx]);
177 ResetTailBits(right_bit_p.z(), &bitmap_[right_idx]);
178 for (size_t i = left_idx + 1; i < right_idx; ++i) {
179 bitmap_[i] = static_cast<uint64_t>(0);
180 }
181}
182
183} // namespace lidar
184} // namespace perception
185} // namespace apollo
const Eigen::Vector2d & max_range() const
Return the max_range_
Definition bitmap2d.h:68
void Set(const Eigen::Vector2d &p)
Set the vector p
Definition bitmap2d.cc:128
int dir_major() const
Return the dir_major_
Definition bitmap2d.h:103
void SetUp(const DirectionMajor dir_major)
Set the dir_major_ and op_dir_major_
Definition bitmap2d.cc:92
DirectionMajor OppositeDirection(const DirectionMajor dir_major)
Opposite direction of direction_major
Definition bitmap2d.cc:28
bool IsExists(const Eigen::Vector2d &p) const
check the vector p is in bitmap_
Definition bitmap2d.cc:115
void Reset(const Eigen::Vector2d &p)
Reset the vector p
Definition bitmap2d.cc:134
const Eigen::Vector2d & min_range() const
Return the min_range_
Definition bitmap2d.h:61
int op_dir_major() const
Return the op_dir_major
Definition bitmap2d.h:110
Eigen::Matrix< size_t, 3, 1 > Vec3ui
Definition bitmap2d.h:33
void Init(const Eigen::Vector2d &min_range, const Eigen::Vector2d &max_range, const Eigen::Vector2d &cell_size)
Init of Bitmap 2D object
Definition bitmap2d.cc:72
bool Check(const Eigen::Vector2d &p) const
Check the vector p
Definition bitmap2d.cc:121
Eigen::Matrix< size_t, 2, 1 > Vec2ui
Definition bitmap2d.h:32
const Eigen::Vector2d & cell_size() const
Return the cell_size_
Definition bitmap2d.h:75
class register implement
Definition arena_queue.h:37