#include <pcl/PCLPointCloud2.h>
#include <pcl/conversions.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <memory>
#include <point_cloud_transport/point_cloud_transport.hpp>
#include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/point_cloud2.hpp>
class CloudiniPluginTestNode : public rclcpp::Node {
public:
CloudiniPluginTestNode() : Node("cloudini_plugin_test") {
this->declare_parameter<std::string>("topic", "/points_pct");
this->declare_parameter<std::string>("transport", "cloudini");
}
void init() {
std::string topic = this->get_parameter("topic").as_string();
std::string transport = this->get_parameter("transport").as_string();
RCLCPP_INFO(this->get_logger(), "Starting Cloudini plugin test node");
RCLCPP_INFO(this->get_logger(), "Subscribing to topic: %s", topic.c_str());
RCLCPP_INFO(this->get_logger(), "Using transport: %s", transport.c_str());
pct_ = std::make_shared<point_cloud_transport::PointCloudTransport>(shared_from_this());
auto transport_hint = std::make_shared<point_cloud_transport::TransportHints>(transport);
subscription_ = pct_->subscribe(
topic, 10, [this](const auto& msg) { this->pointCloudCallback(msg); }, {}, transport_hint.get());
RCLCPP_INFO(this->get_logger(), "Cloudini plugin test node initialized successfully");
}
private:
void pointCloudCallback(const sensor_msgs::msg::PointCloud2::ConstSharedPtr& msg) {
message_count_++;
try {
auto pcl_cloud = std::make_shared<pcl::PCLPointCloud2>();
pcl_conversions::toPCL(*msg, *pcl_cloud);
uint32_t num_points = pcl_cloud->width * pcl_cloud->height;
size_t cloud_size_bytes = pcl_cloud->data.size();
std::string fields_str;
for (const auto& field : pcl_cloud->fields) {
if (!fields_str.empty()) {
fields_str += ", ";
}
fields_str += field.name;
}
if (message_count_ % 10 == 0) {
RCLCPP_INFO(
this->get_logger(), "Received %ld point clouds. %u points (%ux%u), %.2f KB, fields: [%s]", message_count_,
num_points, pcl_cloud->width, pcl_cloud->height, cloud_size_bytes / 1024.0, fields_str.c_str());
}
} catch (const std::exception& e) {
RCLCPP_ERROR(this->get_logger(), "Failed to decode point cloud: %s", e.what());
}
}
std::shared_ptr<point_cloud_transport::PointCloudTransport> pct_;
point_cloud_transport::Subscriber subscription_;
size_t message_count_ = 0;
};
int main(int argc, char** argv) {
rclcpp::init(argc, argv);
auto node = std::make_shared<CloudiniPluginTestNode>();
node->init();
RCLCPP_INFO(node->get_logger(), "Cloudini plugin test node is running...");
RCLCPP_INFO(node->get_logger(), "Press Ctrl+C to exit");
try {
rclcpp::spin(node);
} catch (const std::exception& e) {
RCLCPP_ERROR(node->get_logger(), "Exception during spin: %s", e.what());
}
rclcpp::shutdown();
RCLCPP_INFO(node->get_logger(), "Cloudini plugin test node shutting down");
return 0;
}