42
42
#include < boost/thread/mutex.hpp>
43
43
#include < boost/thread.hpp>
44
44
// PCL includes
45
+ #include < pcl/common/centroid.h>
45
46
#include < pcl/point_types.h>
46
47
#include < pcl_ros/point_cloud.h>
47
48
#include < pcl/visualization/pcl_visualizer.h>
48
49
#include < pcl/features/feature.h>
49
- #include < pcl/common/centroid.h>
50
+ #include < pcl/io/pcd_io.h>
51
+ #include < pcl/filters/voxel_grid.h>
52
+ #include < pcl/filters/passthrough.h>
50
53
51
54
using pcl::visualization::PointCloudColorHandlerGenericField;
52
55
@@ -59,6 +62,8 @@ typedef pcl::PointCloud<PointRGB> PointCloudRGB;
59
62
sensor_msgs::PointCloud2ConstPtr cloud_, cloud_old_;
60
63
boost::mutex m;
61
64
bool viewer_initialized_;
65
+ bool save_cloud_;
66
+ std::string pcd_filename_;
62
67
int counter_;
63
68
64
69
void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& cloud)
@@ -71,6 +76,30 @@ void cloud_cb(const sensor_msgs::PointCloud2ConstPtr& cloud)
71
76
m.unlock ();
72
77
}
73
78
79
+ PointCloudRGB::Ptr filter (PointCloudRGB::Ptr cloud, double voxel_size)
80
+ {
81
+ PointCloudRGB::Ptr cloud_filtered_ptr (new PointCloudRGB);
82
+
83
+ // Downsampling using voxel grid
84
+ pcl::VoxelGrid<PointRGB> grid_;
85
+ PointCloudRGB::Ptr cloud_downsampled_ptr (new PointCloudRGB);
86
+
87
+ grid_.setLeafSize (voxel_size,
88
+ voxel_size,
89
+ voxel_size);
90
+ grid_.setDownsampleAllData (true );
91
+ grid_.setInputCloud (cloud);
92
+ grid_.filter (*cloud_downsampled_ptr);
93
+
94
+ return cloud_downsampled_ptr;
95
+ }
96
+
97
+ void keyboardEventOccurred (const pcl::visualization::KeyboardEvent& event, void * nothing)
98
+ {
99
+ if (event.getKeySym () == " space" && event.keyDown ())
100
+ save_cloud_ = true ;
101
+ }
102
+
74
103
void updateVisualization ()
75
104
{
76
105
PointCloud cloud_xyz;
@@ -80,13 +109,15 @@ void updateVisualization()
80
109
81
110
ros::WallDuration d (0.01 );
82
111
bool rgb = false ;
112
+ // std::vector<sensor_msgs::PointField> fields;
83
113
std::vector<pcl::PCLPointField> fields;
84
114
85
115
// Create the visualizer
86
116
pcl::visualization::PCLVisualizer viewer (" Point Cloud Viewer" );
87
117
88
118
// Add a coordinate system to screen
89
119
viewer.addCoordinateSystem (0.1 );
120
+ viewer.registerKeyboardCallback (&keyboardEventOccurred);
90
121
91
122
while (true )
92
123
{
@@ -127,25 +158,35 @@ void updateVisualization()
127
158
// Initialize the camera view
128
159
if (!viewer_initialized_)
129
160
{
130
- computeMeanAndCovarianceMatrix (cloud_xyz_rgb, covariance_matrix, xyz_centroid);
161
+ pcl:: computeMeanAndCovarianceMatrix (cloud_xyz_rgb, covariance_matrix, xyz_centroid);
131
162
viewer.initCameraParameters ();
132
163
viewer.setCameraPosition (xyz_centroid (0 ), xyz_centroid (1 ), xyz_centroid (2 )+3.0 , 0 , -1 , 0 );
133
164
ROS_INFO_STREAM (" [PointCloudViewer:] Point cloud viewer camera initialized in: [" <<
134
165
xyz_centroid (0 ) << " , " << xyz_centroid (1 ) << " , " << xyz_centroid (2 )+3.0 << " ]" );
135
166
viewer_initialized_ = true ;
136
167
}
137
-
138
168
// Show the point cloud
139
169
pcl::visualization::PointCloudColorHandlerRGBField<PointRGB> color_handler (
140
170
cloud_xyz_rgb.makeShared ());
141
171
viewer.addPointCloud (cloud_xyz_rgb.makeShared (), color_handler, " cloud" );
172
+
173
+ // Save pcd
174
+ if (save_cloud_ && cloud_xyz_rgb.size () > 0 )
175
+ {
176
+ if (pcl::io::savePCDFile (pcd_filename_, cloud_xyz_rgb) == 0 )
177
+ ROS_INFO_STREAM (" [PointCloudViewer:] Pointcloud saved into: " << pcd_filename_);
178
+ else
179
+ ROS_ERROR_STREAM (" [PointCloudViewer:] Problem saving " << pcd_filename_.c_str ());
180
+ save_cloud_ = false ;
181
+ }
182
+
142
183
}
143
184
else
144
185
{
145
186
// Initialize the camera view
146
187
if (!viewer_initialized_)
147
188
{
148
- computeMeanAndCovarianceMatrix (cloud_xyz_rgb, covariance_matrix, xyz_centroid);
189
+ pcl:: computeMeanAndCovarianceMatrix (cloud_xyz_rgb, covariance_matrix, xyz_centroid);
149
190
viewer.initCameraParameters ();
150
191
viewer.setCameraPosition (xyz_centroid (0 ), xyz_centroid (1 ), xyz_centroid (2 )+3.0 , 0 , -1 , 0 );
151
192
ROS_INFO_STREAM (" [PointCloudViewer:] Point cloud viewer camera initialized in: [" <<
@@ -170,8 +211,17 @@ void updateVisualization()
170
211
pcl::visualization::PointCloudColorHandlerCustom<Point> color_handler (
171
212
cloud_xyz.makeShared (), 255 , 0 , 255 );
172
213
}
173
-
174
214
viewer.addPointCloud (cloud_xyz.makeShared (), color_handler, " cloud" );
215
+
216
+ // Save pcd
217
+ if (save_cloud_ && cloud_xyz.size () > 0 )
218
+ {
219
+ if (pcl::io::savePCDFile (pcd_filename_, cloud_xyz) == 0 )
220
+ ROS_INFO_STREAM (" [PointCloudViewer:] Pointcloud saved into: " << pcd_filename_);
221
+ else
222
+ ROS_ERROR_STREAM (" [PointCloudViewer:] Problem saving " << pcd_filename_.c_str ());
223
+ save_cloud_ = false ;
224
+ }
175
225
}
176
226
177
227
counter_++;
@@ -188,9 +238,14 @@ int main(int argc, char** argv)
188
238
{
189
239
ros::init (argc, argv, " pointcloud_viewer" , ros::init_options::NoSigintHandler);
190
240
ros::NodeHandle nh;
241
+ ros::NodeHandle nh_priv (" ~" );
191
242
viewer_initialized_ = false ;
243
+ save_cloud_ = false ;
192
244
counter_ = 0 ;
193
245
246
+ // Read parameters
247
+ nh_priv.param (" pcd_filename" , pcd_filename_, std::string (" pointcloud_file.pcd" ));
248
+
194
249
// Create a ROS subscriber
195
250
ros::Subscriber sub = nh.subscribe (" input" , 30 , cloud_cb);
196
251
0 commit comments