-
Notifications
You must be signed in to change notification settings - Fork 3
/
lec04_visualization.cpp
105 lines (87 loc) · 3.16 KB
/
lec04_visualization.cpp
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
//
// Tutorial Author: shapelim@kaist.ac.kr (임형태)
#include <limits.h> /* PATH_MAX = 4096 */
#include <pcl/point_types.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/common/transforms.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/shapelim/git/pcl_tutorial/materials/kitti00_000000.bin");
Eigen::Matrix4f tf;
tf << 1, 0, 0, 5.0,
0, 1, 0, 0.0,
0, 0, 1, 0.0,
0, 0, 0, 1.0;
pcl::transformPointCloud(*src, *tgt, tf);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr src_colored(new pcl::PointCloud<pcl::PointXYZRGB>);
pcl::PointCloud<pcl::PointXYZRGB>::Ptr tgt_colored(new pcl::PointCloud<pcl::PointXYZRGB>);
// Point cloud XYZ에 RGB 칼라 추가하기
colorize(*src, *src_colored, {255, 0, 0});
colorize(*tgt, *tgt_colored, {0, 255, 0});
/*
* Method 1. PCLVisualizer
*/
pcl::visualization::PCLVisualizer viewer1("Simple Cloud Viewer");
viewer1.addPointCloud<pcl::PointXYZRGB>(src_colored, "src_red");
viewer1.addPointCloud<pcl::PointXYZRGB>(tgt_colored, "tgt_green");
while (!viewer1.wasStopped()) {
viewer1.spinOnce();
}
/*
* Method 2. CloudViewer
* 주의: PCL 버전이 높은 데서만 지원 (TEST: PCL v.1.8)
*/
// pcl::visualization::CloudViewer viewer2("Cloud Viewer");
// viewer2.showCloud(src_colored, "src_red");
// viewer2.showCloud(tgt_colored, "tgt_green");
//
// int cnt = 0;
// while (!viewer2.wasStopped()) {
// cnt++;
// }
}