@@ -63,6 +63,10 @@ bool viewer_initialized_;
63
63
bool save_cloud_;
64
64
std::string pcd_filename_;
65
65
int counter_;
66
+ ros::Timer save_timer_;
67
+
68
+ PointCloud cloud_xyz_;
69
+ PointCloudRGB cloud_xyz_rgb_;
66
70
67
71
void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& cloud)
68
72
{
@@ -75,15 +79,13 @@ void cloud_cb(const sensor_msgs::PointCloud2ConstPtr& cloud)
75
79
void keyboardEventOccurred (const pcl::visualization::KeyboardEvent& event, void * nothing)
76
80
{
77
81
if (event.getKeySym () == " space" && event.keyDown ()) {
78
- ROS_INFO (" SAVING POINTCLOUD, PLEASE WAIT ..." );
82
+ ROS_INFO (" Saving pointcloud, please wait ..." );
79
83
save_cloud_ = true ;
80
84
}
81
85
}
82
86
83
87
void updateVisualization ()
84
88
{
85
- PointCloud cloud_xyz;
86
- PointCloudRGB cloud_xyz_rgb;
87
89
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
88
90
Eigen::Vector4f xyz_centroid;
89
91
@@ -116,13 +118,13 @@ void updateVisualization()
116
118
if (pcl::getFieldIndex (*cloud_, " rgb" ) != -1 )
117
119
{
118
120
rgb = true ;
119
- pcl::fromROSMsg (*cloud_, cloud_xyz_rgb );
121
+ pcl::fromROSMsg (*cloud_, cloud_xyz_rgb_ );
120
122
}
121
123
else
122
124
{
123
125
rgb = false ;
124
- pcl::fromROSMsg (*cloud_, cloud_xyz );
125
- pcl::getFields (cloud_xyz , fields);
126
+ pcl::fromROSMsg (*cloud_, cloud_xyz_ );
127
+ pcl::getFields (cloud_xyz_ , fields);
126
128
}
127
129
cloud_old_ = cloud_;
128
130
m_.unlock ();
@@ -131,13 +133,13 @@ void updateVisualization()
131
133
viewer.removePointCloud (" cloud" );
132
134
133
135
// If no RGB data present, use a simpler white handler
134
- if (rgb && pcl::getFieldIndex (cloud_xyz_rgb , " rgb" , fields) != -1 &&
135
- cloud_xyz_rgb .points [0 ].rgb != 0 )
136
+ if (rgb && pcl::getFieldIndex (cloud_xyz_rgb_ , " rgb" , fields) != -1 &&
137
+ cloud_xyz_rgb_ .points [0 ].rgb != 0 )
136
138
{
137
139
// Initialize the camera view
138
140
if (!viewer_initialized_)
139
141
{
140
- pcl::computeMeanAndCovarianceMatrix (cloud_xyz_rgb , covariance_matrix, xyz_centroid);
142
+ pcl::computeMeanAndCovarianceMatrix (cloud_xyz_rgb_ , covariance_matrix, xyz_centroid);
141
143
viewer.initCameraParameters ();
142
144
viewer.setCameraPosition (xyz_centroid (0 ), xyz_centroid (1 ), xyz_centroid (2 )+3.0 , 0 , -1 , 0 );
143
145
ROS_INFO_STREAM (" [PointCloudViewer:] Point cloud viewer camera initialized in: [" <<
@@ -146,26 +148,15 @@ void updateVisualization()
146
148
}
147
149
// Show the point cloud
148
150
pcl::visualization::PointCloudColorHandlerRGBField<PointRGB> color_handler (
149
- cloud_xyz_rgb.makeShared ());
150
- viewer.addPointCloud (cloud_xyz_rgb.makeShared (), color_handler, " cloud" );
151
-
152
- // Save pcd
153
- if (save_cloud_ && cloud_xyz_rgb.size () > 0 )
154
- {
155
- if (pcl::io::savePCDFile (pcd_filename_, cloud_xyz_rgb) == 0 )
156
- ROS_INFO_STREAM (" [PointCloudViewer:] Pointcloud saved into: " << pcd_filename_);
157
- else
158
- ROS_ERROR_STREAM (" [PointCloudViewer:] Problem saving " << pcd_filename_.c_str ());
159
- save_cloud_ = false ;
160
- }
161
-
151
+ cloud_xyz_rgb_.makeShared ());
152
+ viewer.addPointCloud (cloud_xyz_rgb_.makeShared (), color_handler, " cloud" );
162
153
}
163
154
else
164
155
{
165
156
// Initialize the camera view
166
157
if (!viewer_initialized_)
167
158
{
168
- pcl::computeMeanAndCovarianceMatrix (cloud_xyz_rgb , covariance_matrix, xyz_centroid);
159
+ pcl::computeMeanAndCovarianceMatrix (cloud_xyz_rgb_ , covariance_matrix, xyz_centroid);
169
160
viewer.initCameraParameters ();
170
161
viewer.setCameraPosition (xyz_centroid (0 ), xyz_centroid (1 ), xyz_centroid (2 )+3.0 , 0 , -1 , 0 );
171
162
ROS_INFO_STREAM (" [PointCloudViewer:] Point cloud viewer camera initialized in: [" <<
@@ -174,39 +165,52 @@ void updateVisualization()
174
165
}
175
166
176
167
// Some xyz_rgb point clouds have incorrect rgb field. Detect and convert to xyz.
177
- if (pcl::getFieldIndex (cloud_xyz_rgb , " rgb" , fields) != -1 )
168
+ if (pcl::getFieldIndex (cloud_xyz_rgb_ , " rgb" , fields) != -1 )
178
169
{
179
- if (cloud_xyz_rgb .points [0 ].rgb == 0 )
170
+ if (cloud_xyz_rgb_ .points [0 ].rgb == 0 )
180
171
{
181
- pcl::copyPointCloud (cloud_xyz_rgb, cloud_xyz );
172
+ pcl::copyPointCloud (cloud_xyz_rgb_, cloud_xyz_ );
182
173
}
183
174
}
184
175
185
176
// Show the xyz point cloud
186
- PointCloudColorHandlerGenericField<Point> color_handler (cloud_xyz .makeShared (), " z" );
177
+ PointCloudColorHandlerGenericField<Point> color_handler (cloud_xyz_ .makeShared (), " z" );
187
178
if (!color_handler.isCapable ())
188
179
{
189
180
ROS_WARN_STREAM (" [PointCloudViewer:] Cannot create curvature color handler!" );
190
181
pcl::visualization::PointCloudColorHandlerCustom<Point> color_handler (
191
- cloud_xyz.makeShared (), 255 , 0 , 255 );
192
- }
193
- viewer.addPointCloud (cloud_xyz.makeShared (), color_handler, " cloud" );
194
-
195
- // Save pcd
196
- if (save_cloud_ && cloud_xyz.size () > 0 )
197
- {
198
- if (pcl::io::savePCDFile (pcd_filename_, cloud_xyz) == 0 )
199
- ROS_INFO_STREAM (" [PointCloudViewer:] Pointcloud saved into: " << pcd_filename_);
200
- else
201
- ROS_ERROR_STREAM (" [PointCloudViewer:] Problem saving " << pcd_filename_.c_str ());
202
- save_cloud_ = false ;
182
+ cloud_xyz_.makeShared (), 255 , 0 , 255 );
203
183
}
184
+ viewer.addPointCloud (cloud_xyz_.makeShared (), color_handler, " cloud" );
204
185
}
205
186
206
187
counter_++;
207
188
}
208
189
}
209
190
191
+ void saveCallback (const ros::TimerEvent&)
192
+ {
193
+ if (!save_cloud_) return ;
194
+
195
+ if (cloud_xyz_rgb_.size () > 0 )
196
+ {
197
+ if (pcl::io::savePCDFile (pcd_filename_, cloud_xyz_rgb_) == 0 )
198
+ ROS_INFO_STREAM (" [PointCloudViewer:] Pointcloud saved into: " << pcd_filename_);
199
+ else
200
+ ROS_ERROR_STREAM (" [PointCloudViewer:] Problem saving " << pcd_filename_.c_str ());
201
+ save_cloud_ = false ;
202
+ }
203
+
204
+ if (cloud_xyz_.size () > 0 )
205
+ {
206
+ if (pcl::io::savePCDFile (pcd_filename_, cloud_xyz_) == 0 )
207
+ ROS_INFO_STREAM (" [PointCloudViewer:] Pointcloud saved into: " << pcd_filename_);
208
+ else
209
+ ROS_ERROR_STREAM (" [PointCloudViewer:] Problem saving " << pcd_filename_.c_str ());
210
+ save_cloud_ = false ;
211
+ }
212
+ }
213
+
210
214
void sigIntHandler (int sig)
211
215
{
212
216
exit (0 );
@@ -234,6 +238,8 @@ int main(int argc, char** argv)
234
238
235
239
boost::thread visualization_thread (&updateVisualization);
236
240
241
+ save_timer_ = nh.createTimer (ros::Duration (1 ), &saveCallback);
242
+
237
243
// Spin
238
244
ros::spin ();
239
245
0 commit comments