forked from LimHyungTae/pcl_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlec11_icp.cpp
More file actions
121 lines (102 loc) · 3.87 KB
/
lec11_icp.cpp
File metadata and controls
121 lines (102 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// Tutorial Author: shapelim@kaist.ac.kr (임형태)
#include <pcl/point_types.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/conversions.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/common/transforms.h>
#include <pcl/registration/icp.h>
using namespace std;
pcl::PointCloud<pcl::PointXYZ>::ConstPtr load_bin(const string &filename) {
FILE*file = fopen(filename.c_str(), "rb");
if (!file) {
std::cerr << "Error: failed to load " << filename << std::endl;
return nullptr;
}
std::vector<float> buffer(1000000);
size_t num_points =
fread(reinterpret_cast<char*>(buffer.data()), sizeof(float), buffer.size(), file) / 4;
fclose(file);
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
cloud->resize(num_points);
for (int i = 0; i < num_points; i++) {
auto &pt = cloud->at(i);
pt.x = buffer[i * 4];
pt.y = buffer[i * 4 + 1];
pt.z = buffer[i * 4 + 2];
// pt.intensity = buffer[i * 4 + 3];
}
return cloud;
}
void colorize(const pcl::PointCloud<pcl::PointXYZ> &pc,
pcl::PointCloud<pcl::PointXYZRGB> &pc_colored,
const std::vector<int> &color) {
int N = pc.points.size();
pc_colored.clear();
pcl::PointXYZRGB pt_tmp;
for (int i = 0; i < N; ++i) {
const auto &pt = pc.points[i];
pt_tmp.x = pt.x;
pt_tmp.y = pt.y;
pt_tmp.z = pt.z;
pt_tmp.r = color[0];
pt_tmp.g = color[1];
pt_tmp.b = color[2];
pc_colored.points.emplace_back(pt_tmp);
}
}
int main(int argc, char**argv) {
/*
* Load toy data
*/
pcl::PointCloud<pcl::PointXYZ>::Ptr src(new pcl::PointCloud<pcl::PointXYZ>);
pcl::PointCloud<pcl::PointXYZ>::Ptr tgt(new pcl::PointCloud<pcl::PointXYZ>);
*src = *load_bin("/home/chae/2024/pcl_tutorial/materials/kitti00_000000.bin");
/** Test를 위해 앞으로 2m 전진시킨 target을 만듦 */
Eigen::Matrix4f tf;
tf << 1, 0, 0, 2.0,
0, 1, 0, 0.0,
0, 0, 1, 0.0,
0, 0, 0, 1.0;
pcl::transformPointCloud(*src, *tgt, tf);
/**
* Main
*/
pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp;
icp.setMaxCorrespondenceDistance(1.0);
icp.setTransformationEpsilon(0.003);
icp.setMaximumIterations(1000);
pcl::PointCloud<pcl::PointXYZ>::Ptr align(new pcl::PointCloud<pcl::PointXYZ>);
icp.setInputSource(src);
icp.setInputTarget(tgt);
icp.align(*align);
/*******************************************/
// Set outputs
Eigen::Matrix4f src2tgt = icp.getFinalTransformation();
double score = icp.getFitnessScore();
bool is_converged = icp.hasConverged();
cout<<src2tgt<<endl;
cout<<score<<endl;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr src_colored(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr tgt_colored(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr align_colored(new pcl::PointCloud<pcl::PointXYZRGB>);
colorize(*src, *src_colored, {255, 0, 0});
colorize(*tgt, *tgt_colored, {0, 255, 0});
colorize(*align, *align_colored, {0, 0, 255});
/**
* 결과 visualization 하기
*/
pcl::visualization::CloudViewer viewer("Cloud Viewer");
viewer.showCloud(src_colored, "src_viz");
viewer.showCloud(tgt_colored, "tgt_viz");
viewer.showCloud(align_colored, "align_viz");
// 수렴하면 hasConverged true return
std::cout << "converge?" << bool(is_converged) << std::endl;
int cnt = 0;
while (!viewer.wasStopped()) {
//you can also do cool processing here
//FIXME: Note that this is running in a separate thread from viewerPsycho
//and you should guard against race conditions yourself...
cnt++;
}
}