ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

c++ with pcl_conversions

https://github.com/lucasw/imgui_ros/blob/crystal/imgui_ros/src/test/generate_pointcloud2.cpp

#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>

pcl::PointCloud<pcl::PointXYZRGB> cloud_;
// TODO(lucasw) generate a cube or sphere instead, more interesting than 2d
for (int i = 0; i < num_points_; ++i) {
  const float fr = static_cast<float>(i) / static_cast<float>(num_points_);
  pcl::PointXYZRGB pt;
  pt = pcl::PointXYZRGB(fr * 255, 255 - fr * 255, 18 + fr * 20);
  pt.x = cos(fr * M_PI * 2.0) * 1.0;
  pt.y = sin(fr * M_PI * 2.0) * 1.0;
  pt.z = 0.0;
  cloud_.points.push_back(pt);
}

// intermediate format - this incurs an extra copy?
pcl::PCLPointCloud2 pcl_pc2;
pcl::toPCLPointCloud2(cloud_, pcl_pc2);

pc2_msg_ = std::make_shared<sensor_msgs::msg::PointCloud2>();
pcl_conversions::fromPCL(pcl_pc2, *pc2_msg_);

pc2_msg_->header.frame_id = frame_id_;

...

pc2_msg_->header.stamp = now();

pub_->publish(pc2_msg_);

And in cmake:

add_executable(generate_pointcloud2 src/test/generate_pointcloud2.cpp)
ament_target_dependencies(generate_pointcloud2
  "geometry_msgs"  # maybe don't need this
  "pcl_conversions"
  "rcl_interfaces"
  "rclcpp"
  "rcutils"
  "sensor_msgs"
  "std_msgs"
  "tf2_geometry_msgs"
)
target_link_libraries(generate_pointcloud2
  ${Boost_SYSTEM_LIBRARY}  # pcl_conversions requires this
)

c++ with pcl_conversions

https://github.com/lucasw/imgui_ros/blob/crystal/imgui_ros/src/test/generate_pointcloud2.cpp

#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
 ...
pub_ = create_publisher<sensor_msgs::msg::PointCloud2>("point_cloud");
...
pcl::PointCloud<pcl::PointXYZRGB> cloud_;
// TODO(lucasw) generate a cube or sphere instead, more interesting than 2d
for (int i = 0; i < num_points_; ++i) {
  const float fr = static_cast<float>(i) / static_cast<float>(num_points_);
  pcl::PointXYZRGB pt;
  pt = pcl::PointXYZRGB(fr * 255, 255 - fr * 255, 18 + fr * 20);
  pt.x = cos(fr * M_PI * 2.0) * 1.0;
  pt.y = sin(fr * M_PI * 2.0) * 1.0;
  pt.z = 0.0;
  cloud_.points.push_back(pt);
}

// intermediate format - this incurs an extra copy?
pcl::PCLPointCloud2 pcl_pc2;
pcl::toPCLPointCloud2(cloud_, pcl_pc2);

pc2_msg_ = std::make_shared<sensor_msgs::msg::PointCloud2>();
pcl_conversions::fromPCL(pcl_pc2, *pc2_msg_);
 pc2_msg_->header.frame_id = frame_id_;

"map";
...

pc2_msg_->header.stamp = now();
 pub_->publish(pc2_msg_);

And in cmake:

add_executable(generate_pointcloud2 src/test/generate_pointcloud2.cpp)
ament_target_dependencies(generate_pointcloud2
  "geometry_msgs"  # maybe don't need this
  "pcl_conversions"
  "rcl_interfaces"
  "rclcpp"
  "rcutils"
  "sensor_msgs"
  "std_msgs"
  "tf2_geometry_msgs"
)
target_link_libraries(generate_pointcloud2
  ${Boost_SYSTEM_LIBRARY}  # pcl_conversions requires this
)

c++ with pcl_conversions

https://github.com/lucasw/imgui_ros/blob/crystal/imgui_ros/src/test/generate_pointcloud2.cpp

#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/msg/point_cloud2.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.h>
...
pub_ = create_publisher<sensor_msgs::msg::PointCloud2>("point_cloud");
...
pcl::PointCloud<pcl::PointXYZRGB> cloud_;
// TODO(lucasw) generate a cube or sphere instead, more interesting than 2d
for (int i = 0; i < num_points_; ++i) {
  const float fr = static_cast<float>(i) / static_cast<float>(num_points_);
  pcl::PointXYZRGB pt;
  pt = pcl::PointXYZRGB(fr * 255, 255 - fr * 255, 18 + fr * 20);
  pt.x = cos(fr * M_PI * 2.0) * 1.0;
  pt.y = sin(fr * M_PI * 2.0) * 1.0;
  pt.z = 0.0;
  cloud_.points.push_back(pt);
}

// intermediate format - this incurs an extra copy?
pcl::PCLPointCloud2 pcl_pc2;
pcl::toPCLPointCloud2(cloud_, pcl_pc2);

pc2_msg_ = std::make_shared<sensor_msgs::msg::PointCloud2>();
pcl_conversions::fromPCL(pcl_pc2, pcl::toROSMsg(cloud_, *pc2_msg_);
pc2_msg_->header.frame_id = "map";
...

pc2_msg_->header.stamp = now();
pub_->publish(pc2_msg_);

And in cmake:

add_executable(generate_pointcloud2 src/test/generate_pointcloud2.cpp)
ament_target_dependencies(generate_pointcloud2
  "geometry_msgs"  # maybe don't need this
  "pcl_conversions"
  "rcl_interfaces"
  "rclcpp"
  "rcutils"
  "sensor_msgs"
  "std_msgs"
  "tf2_geometry_msgs"
)
target_link_libraries(generate_pointcloud2
  ${Boost_SYSTEM_LIBRARY}  # pcl_conversions requires this
)