3636#include " rclcpp/rclcpp.hpp"
3737#include " geometry_msgs/msg/vector3_stamped.hpp"
3838#include " pluginlib/class_list_macros.hpp"
39- #include " sensor_msgs/point_cloud_conversion .hpp"
39+ #include " sensor_msgs/point_cloud2_iterator .hpp"
4040
4141PLUGINLIB_EXPORT_CLASS (nav2_costmap_2d::DepthLayer, nav2_costmap_2d::Layer)
4242
@@ -291,13 +291,29 @@ void DepthLayer::depthImageCallback(sensor_msgs::msg::Image::ConstSharedPtr msg)
291291 cv::Mat channels[3 ];
292292 cv::split (points3d, channels);
293293
294- sensor_msgs::msg::PointCloud clearing_points;
295- clearing_points.header .stamp = msg->header .stamp ;
296- clearing_points.header .frame_id = msg->header .frame_id ;
297-
298- sensor_msgs::msg::PointCloud marking_points;
299- marking_points.header .stamp = msg->header .stamp ;
300- marking_points.header .frame_id = msg->header .frame_id ;
294+ // Create clearing cloud
295+ int clearing_points = 0 ;
296+ sensor_msgs::msg::PointCloud2 clearing_msg;
297+ clearing_msg.header .stamp = msg->header .stamp ;
298+ clearing_msg.header .frame_id = msg->header .frame_id ;
299+ sensor_msgs::PointCloud2Modifier clearing_mod (clearing_msg);
300+ clearing_mod.setPointCloud2FieldsByString (1 , " xyz" );
301+ clearing_mod.resize (cv_ptr->image .rows * cv_ptr->image .cols );
302+ sensor_msgs::PointCloud2Iterator<float > clearing_x (clearing_msg, " x" );
303+ sensor_msgs::PointCloud2Iterator<float > clearing_y (clearing_msg, " y" );
304+ sensor_msgs::PointCloud2Iterator<float > clearing_z (clearing_msg, " z" );
305+
306+ // Create marking cloud
307+ int marking_points = 0 ;
308+ sensor_msgs::msg::PointCloud2 marking_msg;
309+ marking_msg.header .stamp = msg->header .stamp ;
310+ marking_msg.header .frame_id = msg->header .frame_id ;
311+ sensor_msgs::PointCloud2Modifier marking_mod (marking_msg);
312+ marking_mod.setPointCloud2FieldsByString (1 , " xyz" );
313+ marking_mod.resize (cv_ptr->image .rows * cv_ptr->image .cols );
314+ sensor_msgs::PointCloud2Iterator<float > marking_x (marking_msg, " x" );
315+ sensor_msgs::PointCloud2Iterator<float > marking_y (marking_msg, " y" );
316+ sensor_msgs::PointCloud2Iterator<float > marking_z (marking_msg, " z" );
301317
302318 // Put points in clearing/marking clouds
303319 for (size_t i = 0 ; i < points3d.rows ; i++)
@@ -306,114 +322,124 @@ void DepthLayer::depthImageCallback(sensor_msgs::msg::Image::ConstSharedPtr msg)
306322 {
307323 // Get next point
308324 geometry_msgs::msg::Point32 current_point;
309- current_point. x = channels[0 ].at <float >(i, j);
310- current_point. y = channels[1 ].at <float >(i, j);
311- current_point. z = channels[2 ].at <float >(i, j);
325+ float x = channels[0 ].at <float >(i, j);
326+ float y = channels[1 ].at <float >(i, j);
327+ float z = channels[2 ].at <float >(i, j);
312328 // Check point validity
313- if (current_point.x != 0.0 &&
314- current_point.y != 0.0 &&
315- current_point.z != 0.0 &&
316- !std::isnan (current_point.x ) &&
317- !std::isnan (current_point.y ) &&
318- !std::isnan (current_point.z ))
329+ if (x != 0.0 && !std::isnan (x) &&
330+ y != 0.0 && !std::isnan (y) &&
331+ z != 0.0 && !std::isnan (z))
319332 {
320- if (clear_with_skipped_rays_)
321- {
322- // If edge rays are to be used for clearing, go ahead and add them now.
323- clearing_points.points .push_back (current_point);
324- }
325-
326- // Do not consider boundary points for obstacles marking since they are very noisy.
327- if (i < skip_rays_top_ ||
328- i >= points3d.rows - skip_rays_bottom_ ||
329- j < skip_rays_left_ ||
330- j >= points3d.cols - skip_rays_right_)
331- {
332- continue ;
333- }
334-
335- if (!clear_with_skipped_rays_)
336- {
337- // If edge rays are not to be used for clearing, only add them after the edge check.
338- clearing_points.points .push_back (current_point);
339- }
340-
341333 // Check if point is part of the ground plane
342334 if (fabs (ground_plane[0 ] * current_point.x +
343335 ground_plane[1 ] * current_point.y +
344336 ground_plane[2 ] * current_point.z +
345337 ground_plane[3 ]) <= observations_threshold_)
346338 {
347- continue ; // Do not mark points near the floor.
339+ if (clear_with_skipped_rays_)
340+ {
341+ // If edge rays are to be used for clearing, go ahead and add them now.
342+ *clearing_x = x;
343+ *clearing_y = y;
344+ *clearing_z = z;
345+ ++clearing_points;
346+ ++clearing_x;
347+ ++clearing_y;
348+ ++clearing_z;
349+ }
350+ else if (i < skip_rays_top_ ||
351+ i >= points3d.rows - skip_rays_bottom_ ||
352+ j < skip_rays_left_ ||
353+ j >= points3d.cols - skip_rays_right_)
354+ {
355+ // Do not consider boundary points for clearing since they are very noisy.
356+ }
357+ else
358+ {
359+ // If edge rays are not to be used for clearing, only add them after the edge check.
360+ *clearing_x = x;
361+ *clearing_y = y;
362+ *clearing_z = z;
363+ ++clearing_points;
364+ ++clearing_x;
365+ ++clearing_y;
366+ ++clearing_z;
367+ }
348368 }
349-
350- // Check for outliers, mark non-outliers as obstacles.
351- int num_valid = 0 ;
352- for (int x = -1 ; x < 2 ; x++)
369+ else // is marking
353370 {
354- for (int y = -1 ; y < 2 ; y++)
371+ // Do not consider boundary points for obstacles marking since they are very noisy.
372+ if (i < skip_rays_top_ ||
373+ i >= points3d.rows - skip_rays_bottom_ ||
374+ j < skip_rays_left_ ||
375+ j >= points3d.cols - skip_rays_right_)
355376 {
356- if (x == 0 && y == 0 )
357- {
358- continue ;
359- }
360- float px = channels[0 ].at <float >(i+x, j+y);
361- float py = channels[1 ].at <float >(i+x, j+y);
362- float pz = channels[2 ].at <float >(i+x, j+y);
363- if (px != 0.0 && py != 0.0 && pz != 0.0 &&
364- !std::isnan (px) && !std::isnan (py) && !std::isnan (pz))
377+ continue ;
378+ }
379+
380+ // Check for outliers, mark non-outliers as obstacles.
381+ int num_valid = 0 ;
382+ for (int x = -1 ; x < 2 ; x++)
383+ {
384+ for (int y = -1 ; y < 2 ; y++)
365385 {
366- if ( fabs (px - current_point.x ) < 0.1 &&
367- fabs (py - current_point.y ) < 0.1 &&
368- fabs (pz - current_point.z ) < 0.1 )
386+ if (x == 0 && y == 0 )
369387 {
370- num_valid++ ;
388+ continue ;
371389 }
372- }
373- } // for y
374- } // for x
390+ float px = channels[0 ].at <float >(i+x, j+y);
391+ float py = channels[1 ].at <float >(i+x, j+y);
392+ float pz = channels[2 ].at <float >(i+x, j+y);
393+ if (px != 0.0 && py != 0.0 && pz != 0.0 &&
394+ !std::isnan (px) && !std::isnan (py) && !std::isnan (pz))
395+ {
396+ if ( fabs (px - current_point.x ) < 0.1 &&
397+ fabs (py - current_point.y ) < 0.1 &&
398+ fabs (pz - current_point.z ) < 0.1 )
399+ {
400+ num_valid++;
401+ }
402+ }
403+ } // for y
404+ } // for x
375405
376- if (num_valid >= 7 )
377- {
378- marking_points.points .push_back (current_point);
406+ if (num_valid >= 7 )
407+ {
408+ *marking_x = x;
409+ *marking_y = y;
410+ *marking_z = z;
411+ ++marking_points;
412+ ++marking_x;
413+ ++marking_y;
414+ ++marking_z;
415+ }
379416 }
380417 } // for j (y)
381418 } // for i (x)
382419 }
383420
384- sensor_msgs::msg::PointCloud2 clearing_cloud;
385- if (!sensor_msgs::convertPointCloudToPointCloud2 (clearing_points, clearing_cloud))
386- {
387- RCLCPP_ERROR (LOGGER, " Failed to convert a PointCloud to a PointCloud2, dropping message" );
388- return ;
389- }
421+ clearing_mod.resize (clearing_points);
422+ marking_mod.resize (marking_points);
390423
391424 // Publish and buffer our clearing point cloud
392425 if (publish_observations_)
393426 {
394- clearing_pub_->publish (clearing_cloud );
427+ clearing_pub_->publish (clearing_msg );
395428 }
396429
397430 // buffer the ground plane observation
398431 clearing_buf_->lock ();
399- clearing_buf_->bufferCloud (clearing_cloud );
432+ clearing_buf_->bufferCloud (clearing_msg );
400433 clearing_buf_->unlock ();
401434
402- sensor_msgs::msg::PointCloud2 marking_cloud;
403- if (!sensor_msgs::convertPointCloudToPointCloud2 (marking_points, marking_cloud))
404- {
405- RCLCPP_ERROR (LOGGER, " Failed to convert a PointCloud to a PointCloud2, dropping message" );
406- return ;
407- }
408-
409435 // Publish and buffer our marking point cloud
410436 if (publish_observations_)
411437 {
412- marking_pub_->publish (marking_cloud );
438+ marking_pub_->publish (marking_msg );
413439 }
414440
415441 marking_buf_->lock ();
416- marking_buf_->bufferCloud (marking_cloud );
442+ marking_buf_->bufferCloud (marking_msg );
417443 marking_buf_->unlock ();
418444}
419445
0 commit comments