Skip to content

Commit 3151d54

Browse files
author
plnegre
committed
Fix save problem in pointlcoud viewer
1 parent df08400 commit 3151d54

File tree

2 files changed

+48
-42
lines changed

2 files changed

+48
-42
lines changed

pointcloud_tools/src/pointcloud_mapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PointCloudMapper
3535
cloud_sub_ = nh_.subscribe<PointCloud>("input", 1, &PointCloudMapper::callback, this);
3636
bool latched = true;
3737
cloud_pub_ = nh_priv_.advertise<PointCloud>("output", 1, latched);
38-
pub_timer_ = nh_.createTimer(ros::Duration(10.0), &PointCloudMapper::publishCallback, this);
38+
pub_timer_ = nh_.createTimer(ros::Duration(3.0), &PointCloudMapper::publishCallback, this);
3939
}
4040

4141
void callback(const PointCloud::ConstPtr& cloud)
@@ -73,9 +73,9 @@ class PointCloudMapper
7373

7474
void publishCallback(const ros::TimerEvent&)
7575
{
76-
// Publish the accumulated cloud if last publication was more than 10 seconds before.
76+
// Publish the accumulated cloud if last publication was more than 5 seconds before.
7777
ros::WallDuration elapsed_time = ros::WallTime::now() - last_pub_time_;
78-
if (cloud_pub_.getNumSubscribers() > 0 && elapsed_time.toSec() > 10.0)
78+
if (cloud_pub_.getNumSubscribers() > 0 && elapsed_time.toSec() > 5.0)
7979
cloud_pub_.publish(accumulated_cloud_);
8080
}
8181

pointcloud_tools/src/pointcloud_viewer.cpp

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ bool viewer_initialized_;
6363
bool save_cloud_;
6464
std::string pcd_filename_;
6565
int counter_;
66+
ros::Timer save_timer_;
67+
68+
PointCloud cloud_xyz_;
69+
PointCloudRGB cloud_xyz_rgb_;
6670

6771
void cloud_cb(const sensor_msgs::PointCloud2ConstPtr& cloud)
6872
{
@@ -75,15 +79,13 @@ void cloud_cb(const sensor_msgs::PointCloud2ConstPtr& cloud)
7579
void keyboardEventOccurred(const pcl::visualization::KeyboardEvent& event, void* nothing)
7680
{
7781
if (event.getKeySym() == "space" && event.keyDown()) {
78-
ROS_INFO("SAVING POINTCLOUD, PLEASE WAIT...");
82+
ROS_INFO("Saving pointcloud, please wait...");
7983
save_cloud_ = true;
8084
}
8185
}
8286

8387
void updateVisualization()
8488
{
85-
PointCloud cloud_xyz;
86-
PointCloudRGB cloud_xyz_rgb;
8789
EIGEN_ALIGN16 Eigen::Matrix3f covariance_matrix;
8890
Eigen::Vector4f xyz_centroid;
8991

@@ -116,13 +118,13 @@ void updateVisualization()
116118
if(pcl::getFieldIndex(*cloud_, "rgb") != -1)
117119
{
118120
rgb = true;
119-
pcl::fromROSMsg(*cloud_, cloud_xyz_rgb);
121+
pcl::fromROSMsg(*cloud_, cloud_xyz_rgb_);
120122
}
121123
else
122124
{
123125
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);
126128
}
127129
cloud_old_ = cloud_;
128130
m_.unlock();
@@ -131,13 +133,13 @@ void updateVisualization()
131133
viewer.removePointCloud("cloud");
132134

133135
// 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)
136138
{
137139
// Initialize the camera view
138140
if(!viewer_initialized_)
139141
{
140-
pcl::computeMeanAndCovarianceMatrix(cloud_xyz_rgb, covariance_matrix, xyz_centroid);
142+
pcl::computeMeanAndCovarianceMatrix(cloud_xyz_rgb_, covariance_matrix, xyz_centroid);
141143
viewer.initCameraParameters();
142144
viewer.setCameraPosition(xyz_centroid(0), xyz_centroid(1), xyz_centroid(2)+3.0, 0, -1, 0);
143145
ROS_INFO_STREAM("[PointCloudViewer:] Point cloud viewer camera initialized in: [" <<
@@ -146,26 +148,15 @@ void updateVisualization()
146148
}
147149
// Show the point cloud
148150
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");
162153
}
163154
else
164155
{
165156
// Initialize the camera view
166157
if(!viewer_initialized_)
167158
{
168-
pcl::computeMeanAndCovarianceMatrix(cloud_xyz_rgb, covariance_matrix, xyz_centroid);
159+
pcl::computeMeanAndCovarianceMatrix(cloud_xyz_rgb_, covariance_matrix, xyz_centroid);
169160
viewer.initCameraParameters();
170161
viewer.setCameraPosition(xyz_centroid(0), xyz_centroid(1), xyz_centroid(2)+3.0, 0, -1, 0);
171162
ROS_INFO_STREAM("[PointCloudViewer:] Point cloud viewer camera initialized in: [" <<
@@ -174,39 +165,52 @@ void updateVisualization()
174165
}
175166

176167
// 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)
178169
{
179-
if(cloud_xyz_rgb.points[0].rgb == 0)
170+
if(cloud_xyz_rgb_.points[0].rgb == 0)
180171
{
181-
pcl::copyPointCloud(cloud_xyz_rgb, cloud_xyz);
172+
pcl::copyPointCloud(cloud_xyz_rgb_, cloud_xyz_);
182173
}
183174
}
184175

185176
// Show the xyz point cloud
186-
PointCloudColorHandlerGenericField<Point> color_handler (cloud_xyz.makeShared(), "z");
177+
PointCloudColorHandlerGenericField<Point> color_handler (cloud_xyz_.makeShared(), "z");
187178
if (!color_handler.isCapable ())
188179
{
189180
ROS_WARN_STREAM("[PointCloudViewer:] Cannot create curvature color handler!");
190181
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);
203183
}
184+
viewer.addPointCloud(cloud_xyz_.makeShared(), color_handler, "cloud");
204185
}
205186

206187
counter_++;
207188
}
208189
}
209190

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+
210214
void sigIntHandler(int sig)
211215
{
212216
exit(0);
@@ -234,6 +238,8 @@ int main(int argc, char** argv)
234238

235239
boost::thread visualization_thread(&updateVisualization);
236240

241+
save_timer_ = nh.createTimer(ros::Duration(1), &saveCallback);
242+
237243
// Spin
238244
ros::spin();
239245

0 commit comments

Comments
 (0)