Skip to content

Commit 3efc1b3

Browse files
authored
Merge pull request #25 from KOKIAOKI/feature/fix_discrete_transformation
Feature/fix discrete transformation
2 parents eccfe0b + ccda680 commit 3efc1b3

File tree

9 files changed

+208
-314
lines changed

9 files changed

+208
-314
lines changed

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ include_directories(
2626
add_library(cpu_bbs3d
2727
SHARED
2828
bbs3d/src/cpu_bbs3d/bbs3d.cpp
29-
bbs3d/src/cpu_bbs3d/discreate_transformation.cpp
3029
bbs3d/src/cpu_bbs3d/voxelmaps.cpp
3130
bbs3d/src/cpu_bbs3d/voxelmaps_io.cpp
3231
)
@@ -38,7 +37,6 @@ if(BUILD_CUDA)
3837
SHARED
3938
bbs3d/src/gpu_bbs3d/bbs3d.cu
4039
bbs3d/src/gpu_bbs3d/calc_score.cu
41-
bbs3d/src/gpu_bbs3d/discreate_transformation.cu
4240
bbs3d/src/gpu_bbs3d/voxelmaps.cu
4341
bbs3d/src/gpu_bbs3d/stream_manager/check_error.cu
4442
bbs3d/src/gpu_bbs3d/voxelmaps_io.cu

bbs3d/include/cpu_bbs3d/bbs3d.hpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <chrono>
88
#include <Eigen/Dense>
99

10+
#include <discrete_transformation/discrete_transformation.hpp>
11+
1012
namespace cpu {
1113
class VoxelMaps;
1214

@@ -16,34 +18,6 @@ struct AngularInfo {
1618
Eigen::Vector3d min_rpy;
1719
};
1820

19-
struct DiscreteTransformation {
20-
public:
21-
DiscreteTransformation();
22-
DiscreteTransformation(int score);
23-
DiscreteTransformation(int score, int level, double resolution, double x, double y, double z, double roll, double pitch, double yaw);
24-
~DiscreteTransformation();
25-
26-
bool operator<(const DiscreteTransformation& rhs) const;
27-
28-
bool is_leaf() const;
29-
30-
Eigen::Matrix4d create_matrix() const;
31-
32-
std::vector<DiscreteTransformation> branch(const int child_level, const double child_res, const int v_rate, const AngularInfo& ang_info) const;
33-
34-
void calc_score(const std::vector<Eigen::Vector4i>& buckets, const int max_bucket_scan_count, const std::vector<Eigen::Vector3d>& points);
35-
36-
int score;
37-
int level;
38-
double resolution;
39-
double x;
40-
double y;
41-
double z;
42-
double roll;
43-
double pitch;
44-
double yaw;
45-
};
46-
4721
class BBS3D {
4822
public:
4923
BBS3D();
@@ -118,7 +92,16 @@ class BBS3D {
11892
private:
11993
void calc_angular_info(std::vector<AngularInfo>& ang_info_vec);
12094

121-
std::vector<DiscreteTransformation> create_init_transset(const AngularInfo& init_ang_info);
95+
std::vector<DiscreteTransformation<double>> create_init_transset(const AngularInfo& init_ang_info);
96+
97+
void calc_score(
98+
DiscreteTransformation<double>& trans,
99+
const double trans_res,
100+
const Eigen::Vector3d& rpy_res,
101+
const Eigen::Vector3d& min_rpy,
102+
const std::vector<Eigen::Vector4i>& buckets,
103+
const int max_bucket_scan_count,
104+
const std::vector<Eigen::Vector3d>& points);
122105

123106
private:
124107
Eigen::Matrix4d global_pose_;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#pragma once
2+
#include <Eigen/Core>
3+
4+
template <typename T>
5+
using Vector3 = Eigen::Matrix<T, 3, 1>;
6+
7+
template <typename T>
8+
using Matrix4 = Eigen::Matrix<T, 4, 4>;
9+
10+
template <typename T>
11+
class DiscreteTransformation {
12+
public:
13+
DiscreteTransformation() : score(0), level(0), x(0), y(0), z(0), roll(0), pitch(0), yaw(0) {}
14+
15+
DiscreteTransformation(int score) : score(score), level(0), x(0), y(0), z(0), roll(0), pitch(0), yaw(0) {}
16+
17+
DiscreteTransformation(int score, int level, int x, int y, int z, int roll, int pitch, int yaw)
18+
: score(score),
19+
level(level),
20+
x(x),
21+
y(y),
22+
z(z),
23+
roll(roll),
24+
pitch(pitch),
25+
yaw(yaw) {}
26+
27+
~DiscreteTransformation() {}
28+
29+
bool operator<(const DiscreteTransformation& rhs) const { return score < rhs.score; }
30+
31+
bool is_leaf() const { return level == 0; }
32+
33+
Matrix4<T> create_matrix(const T trans_res, const Vector3<T>& rpy_res, const Vector3<T>& min_rpy) {
34+
Eigen::Translation<T, 3> translation(x * trans_res, y * trans_res, z * trans_res);
35+
Eigen::AngleAxis<T> rollAngle(roll * rpy_res.x() + min_rpy.x(), Vector3<T>::UnitX());
36+
Eigen::AngleAxis<T> pitchAngle(pitch * rpy_res.y() + min_rpy.y(), Vector3<T>::UnitY());
37+
Eigen::AngleAxis<T> yawAngle(yaw * rpy_res.z() + min_rpy.z(), Vector3<T>::UnitZ());
38+
return (translation * yawAngle * pitchAngle * rollAngle).matrix();
39+
}
40+
41+
void branch(std::vector<DiscreteTransformation>& b, const int child_level, const int v_rate, const Eigen::Vector3i& num_division) {
42+
for (int i = 0; i < v_rate; i++) {
43+
for (int j = 0; j < v_rate; j++) {
44+
for (int k = 0; k < v_rate; k++) {
45+
for (int l = 0; l < num_division.x(); l++) {
46+
for (int m = 0; m < num_division.y(); m++) {
47+
for (int n = 0; n < num_division.z(); n++) {
48+
b.emplace_back(DiscreteTransformation(
49+
0,
50+
child_level,
51+
x * v_rate + i,
52+
y * v_rate + j,
53+
z * v_rate + k,
54+
roll * num_division.x() + l,
55+
pitch * num_division.y() + m,
56+
yaw * num_division.z() + n));
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
std::vector<DiscreteTransformation> branch(const int child_level, const int v_rate, const Eigen::Vector3i& num_division) {
66+
std::vector<DiscreteTransformation> b;
67+
b.reserve(v_rate * v_rate * v_rate * num_division.x() * num_division.y() * num_division.z());
68+
for (int i = 0; i < v_rate; i++) {
69+
for (int j = 0; j < v_rate; j++) {
70+
for (int k = 0; k < v_rate; k++) {
71+
for (int l = 0; l < num_division.x(); l++) {
72+
for (int m = 0; m < num_division.y(); m++) {
73+
for (int n = 0; n < num_division.z(); n++) {
74+
b.emplace_back(DiscreteTransformation(
75+
0,
76+
child_level,
77+
x * v_rate + i,
78+
y * v_rate + j,
79+
z * v_rate + k,
80+
roll * num_division.x() + l,
81+
pitch * num_division.y() + m,
82+
yaw * num_division.z() + n));
83+
}
84+
}
85+
}
86+
}
87+
}
88+
}
89+
return b;
90+
}
91+
92+
public:
93+
int score;
94+
int level;
95+
int x;
96+
int y;
97+
int z;
98+
int roll;
99+
int pitch;
100+
int yaw;
101+
};

bbs3d/include/gpu_bbs3d/bbs3d.cuh

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <chrono>
77
#include <Eigen/Dense>
88

9+
#include <discrete_transformation/discrete_transformation.hpp>
10+
911
// thrust
1012
#include <cuda_runtime.h>
1113
#include <thrust/host_vector.h>
@@ -20,33 +22,6 @@ struct AngularInfo {
2022
Eigen::Vector3f min_rpy;
2123
};
2224

23-
struct DiscreteTransformation {
24-
public:
25-
DiscreteTransformation();
26-
DiscreteTransformation(int score);
27-
DiscreteTransformation(int score, int level, float resolution, float x, float y, float z, float roll, float pitch, float yaw);
28-
~DiscreteTransformation();
29-
30-
bool operator<(const DiscreteTransformation& rhs) const;
31-
32-
bool is_leaf() const;
33-
34-
Eigen::Matrix4f create_matrix();
35-
36-
void branch(std::vector<DiscreteTransformation>& b, const int child_level, const float child_res, const int v_rate, const AngularInfo& ang_info);
37-
38-
public:
39-
int score;
40-
int level;
41-
float resolution;
42-
float x;
43-
float y;
44-
float z;
45-
float roll;
46-
float pitch;
47-
float yaw;
48-
};
49-
5025
class BBS3D {
5126
public:
5227
BBS3D();
@@ -112,9 +87,11 @@ public:
11287
private:
11388
void calc_angular_info(std::vector<AngularInfo>& ang_info_vec);
11489

115-
std::vector<DiscreteTransformation> create_init_transset(const AngularInfo& init_ang_info);
90+
std::vector<DiscreteTransformation<float>> create_init_transset(const AngularInfo& init_ang_info);
11691

117-
std::vector<DiscreteTransformation> calc_scores(const std::vector<DiscreteTransformation>& h_transset);
92+
std::vector<DiscreteTransformation<float>> calc_scores(
93+
const std::vector<DiscreteTransformation<float>>& h_transset,
94+
thrust::device_vector<AngularInfo>& d_ang_info_vec);
11895

11996
// pcd iof
12097
bool load_voxel_params(const std::string& voxelmaps_folder_path);

bbs3d/src/cpu_bbs3d/bbs3d.cpp

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,22 @@ void BBS3D::calc_angular_info(std::vector<AngularInfo>& ang_info_vec) {
106106
}
107107
}
108108

109-
std::vector<DiscreteTransformation> BBS3D::create_init_transset(const AngularInfo& init_ang_info) {
109+
std::vector<DiscreteTransformation<double>> BBS3D::create_init_transset(const AngularInfo& init_ang_info) {
110110
const int init_transset_size = (init_tx_range_.second - init_tx_range_.first + 1) * (init_ty_range_.second - init_ty_range_.first + 1) *
111111
(init_tz_range_.second - init_tz_range_.first + 1) * (init_ang_info.num_division.x()) *
112112
(init_ang_info.num_division.y()) * (init_ang_info.num_division.z());
113113

114114
const int max_level = voxelmaps_ptr_->get_max_level();
115-
const double init_trans_res = voxelmaps_ptr_->voxelmaps_res_[max_level];
116115

117-
std::vector<DiscreteTransformation> transset;
116+
std::vector<DiscreteTransformation<double>> transset;
118117
transset.reserve(init_transset_size);
119118
for (int tx = init_tx_range_.first; tx <= init_tx_range_.second; tx++) {
120119
for (int ty = init_ty_range_.first; ty <= init_ty_range_.second; ty++) {
121120
for (int tz = init_tz_range_.first; tz <= init_tz_range_.second; tz++) {
122121
for (int roll = 0; roll < init_ang_info.num_division.x(); roll++) {
123122
for (int pitch = 0; pitch < init_ang_info.num_division.y(); pitch++) {
124123
for (int yaw = 0; yaw < init_ang_info.num_division.z(); yaw++) {
125-
transset.emplace_back(DiscreteTransformation(
126-
0,
127-
max_level,
128-
init_trans_res,
129-
tx * init_trans_res,
130-
ty * init_trans_res,
131-
tz * init_trans_res,
132-
roll * init_ang_info.rpy_res.x() + init_ang_info.min_rpy.x(),
133-
pitch * init_ang_info.rpy_res.y() + init_ang_info.min_rpy.y(),
134-
yaw * init_ang_info.rpy_res.z() + init_ang_info.min_rpy.z()));
124+
transset.emplace_back(DiscreteTransformation<double>(0, max_level, tx, ty, tz, roll, pitch, yaw));
135125
}
136126
}
137127
}
@@ -141,6 +131,40 @@ std::vector<DiscreteTransformation> BBS3D::create_init_transset(const AngularInf
141131
return transset;
142132
}
143133

134+
void BBS3D::calc_score(
135+
DiscreteTransformation<double>& trans,
136+
const double trans_res,
137+
const Eigen::Vector3d& rpy_res,
138+
const Eigen::Vector3d& min_rpy,
139+
const std::vector<Eigen::Vector4i>& buckets,
140+
const int max_bucket_scan_count,
141+
const std::vector<Eigen::Vector3d>& points) {
142+
const int num_buckets = buckets.size();
143+
const double inv_res = 1.0 / trans_res;
144+
Eigen::Transform<double, 3, Eigen::Affine> transform;
145+
transform = trans.create_matrix(trans_res, rpy_res, min_rpy);
146+
147+
for (int i = 0; i < points.size(); i++) {
148+
const Eigen::Vector3d transed_point = transform * points[i];
149+
const Eigen::Vector3i coord = (transed_point.array() * inv_res).floor().cast<int>();
150+
const std::uint32_t hash = (coord[0] * 73856093) ^ (coord[1] * 19349669) ^ (coord[2] * 83492791);
151+
152+
for (int j = 0; j < max_bucket_scan_count; j++) {
153+
const std::uint32_t bucket_index = (hash + j) % num_buckets;
154+
const Eigen::Vector4i& bucket = buckets[bucket_index];
155+
156+
if (bucket.x() != coord.x() || bucket.y() != coord.y() || bucket.z() != coord.z()) {
157+
continue;
158+
}
159+
160+
if (bucket.w() == 1) {
161+
trans.score++;
162+
break;
163+
}
164+
}
165+
}
166+
}
167+
144168
void BBS3D::localize() {
145169
// Calc localize time limit
146170
const auto start_time = std::chrono::system_clock::now();
@@ -150,7 +174,7 @@ void BBS3D::localize() {
150174
best_score_ = 0;
151175
const int score_threshold = std::floor(src_points_.size() * score_threshold_percentage_);
152176
int best_score = score_threshold;
153-
DiscreteTransformation best_trans(best_score);
177+
DiscreteTransformation<double> best_trans(best_score);
154178

155179
// Preapre initial transset
156180
const int max_bucket_scan_count = voxelmaps_ptr_->get_max_bucket_scan_count();
@@ -161,12 +185,15 @@ void BBS3D::localize() {
161185

162186
// Calc initial transset scores
163187
const auto& top_buckets = voxelmaps_ptr_->multi_buckets_[max_level];
188+
double init_trans_res = voxelmaps_ptr_->voxelmaps_res_[max_level];
189+
const Eigen::Vector3d& rpy_res = ang_info_vec[max_level].rpy_res;
190+
const Eigen::Vector3d& min_rpy = ang_info_vec[max_level].min_rpy;
164191
#pragma omp parallel for num_threads(num_threads_)
165192
for (int i = 0; i < init_transset.size(); i++) {
166-
init_transset[i].calc_score(top_buckets, max_bucket_scan_count, src_points_);
193+
calc_score(init_transset[i], init_trans_res, rpy_res, min_rpy, top_buckets, max_bucket_scan_count, src_points_);
167194
}
168195

169-
std::priority_queue<DiscreteTransformation> trans_queue(init_transset.begin(), init_transset.end());
196+
std::priority_queue<DiscreteTransformation<double>> trans_queue(init_transset.begin(), init_transset.end());
170197

171198
while (!trans_queue.empty()) {
172199
if (use_timeout_ && std::chrono::system_clock::now() > time_limit) {
@@ -187,14 +214,17 @@ void BBS3D::localize() {
187214
best_score = trans.score;
188215
} else {
189216
const int child_level = trans.level - 1;
190-
const double child_res = trans.resolution * inv_v_rate_;
191-
auto children = trans.branch(child_level, child_res, static_cast<int>(v_rate_), ang_info_vec[child_level]);
217+
const Eigen::Vector3i& num_division = ang_info_vec[child_level].num_division;
218+
const Eigen::Vector3d& rpy_res = ang_info_vec[child_level].rpy_res;
219+
const Eigen::Vector3d& min_rpy = ang_info_vec[child_level].min_rpy;
220+
auto children = trans.branch(child_level, static_cast<int>(v_rate_), num_division);
192221

193222
const auto& buckets = voxelmaps_ptr_->multi_buckets_[child_level];
223+
double trans_res = voxelmaps_ptr_->voxelmaps_res_[child_level];
194224

195225
#pragma omp parallel for num_threads(num_threads_)
196226
for (int i = 0; i < children.size(); i++) {
197-
children[i].calc_score(buckets, max_bucket_scan_count, src_points_);
227+
calc_score(children[i], trans_res, rpy_res, min_rpy, buckets, max_bucket_scan_count, src_points_);
198228
}
199229

200230
for (const auto& child : children) {
@@ -218,7 +248,8 @@ void BBS3D::localize() {
218248
return;
219249
}
220250

221-
global_pose_ = best_trans.create_matrix();
251+
double min_res = voxelmaps_ptr_->get_min_res();
252+
global_pose_ = best_trans.create_matrix(min_res, ang_info_vec[0].rpy_res, ang_info_vec[0].min_rpy);
222253
best_score_ = best_score;
223254
has_timed_out_ = false;
224255
has_localized_ = true;

0 commit comments

Comments
 (0)