-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (125 loc) · 3.63 KB
/
main.cpp
File metadata and controls
149 lines (125 loc) · 3.63 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Simple plane fitting with PCL/C++:
*
* - Region Growing(normal based clustering)
* - RANSAC Plane Fitting
* - Convex Hull to get the outline
*
* aditya_a@pretia.co.jp
*
* usage: ./simple_plane_fitting [data/0.pcd] [true]
*/
#include "types.hpp"
#include "clustering.hpp"
#include "plane_fitting.hpp"
#include "util.hpp"
#include <pcl/common/common.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
int main(int argc, char *argv[])
{
std::string filename = "../data/table.pcd";
bool visualize = true;
float downsample_size = 0.00f;
bool colorless = false;
if (argc > 1)
{
filename = argv[1];
}
if (argc > 2 && std::string(argv[2]) == "false")
{
visualize = false;
}
else
{
visualize = true;
}
if (argc > 3)
{
downsample_size = std::stof(argv[3]);
}
if (argc > 4)
{
colorless = true;
}
std::cout << "Data: " << filename << std::endl
<< "Visualize: " << visualize << std::endl
<< "Downsample: " << downsample_size << std::endl
<< "Colorless: " << colorless << std::endl;
// load file
cloud_ptr_t cloud_ptr(new cloud_t);
pcl::io::loadPCDFile(filename, *cloud_ptr);
// validate file
if (cloud_ptr->points.empty())
{
std::cout << "Data doesn't exist" << std::endl;
return -1;
}
if (downsample_size > 0.001f)
{
downsample(cloud_ptr, downsample_size);
}
if (colorless)
{ // turn to red
for (auto &pt : cloud_ptr->points)
{
pt.r = 255;
pt.g = pt.b = 0;
}
}
pcl::visualization::PCLVisualizer::Ptr viewer_ptr;
if (visualize)
{
viewer_ptr = boost::make_shared<pcl::visualization::PCLVisualizer>();
viewer_ptr->addCoordinateSystem(0.1f);
viewer_ptr->addPointCloud(cloud_ptr);
std::cout << "Visualizing the original, press [e] or [q] to continue.." << std::endl;
viewer_ptr->spin();
}
// clustering
auto clustering_result = run_clustering(cloud_ptr);
if (!clustering_result.success)
{
std::cout << "Clustering failed" << std::endl;
}
if (visualize)
{
viewer_ptr->updatePointCloud(clustering_result.colored_cloud);
std::cout << "Visualizing the cluster, press [e] or [q] to continue.." << std::endl;
viewer_ptr->spin();
}
// compute plane
std::vector<PlaneInfo> plane_infos;
plane_infos.reserve(clustering_result.clusters.size());
for (auto cluster : clustering_result.clusters)
{
cloud_ptr_t cluster_cloud_ptr(new cloud_t);
pcl::copyPointCloud(*cloud_ptr, cluster, *cluster_cloud_ptr);
auto plane_data = run_plane_fitting(cluster_cloud_ptr);
if (!plane_data.success)
continue;
plane_infos.push_back(plane_data);
}
if (visualize)
{
float r = 0.0, g = 1.0, b = 0.0;
int line_cnt = 0;
for (const auto &plane : plane_infos)
{
const auto &pts = plane.hull_cloud_ptr->points;
const auto &sz = pts.size();
for (int i = 0; i < sz; ++i)
{
viewer_ptr->addLine(pts[i], pts[(i + 1) % sz], r, g, b, std::to_string(line_cnt++) + "L");
}
}
if (colorless)
viewer_ptr->updatePointCloud(clustering_result.colored_cloud);
else
viewer_ptr->updatePointCloud(cloud_ptr);
std::cout << "Visualizing the original & plane, press [e] or [q] to continue.." << std::endl;
viewer_ptr->spin();
}
return 0;
}