27#ifndef EASYNAV_SENSORS__TYPES__POINTPERCEPTION_HPP_
28#define EASYNAV_SENSORS__TYPES__POINTPERCEPTION_HPP_
38#include "tf2/LinearMath/Transform.hpp"
39#include "pcl/point_cloud.h"
40#include "pcl/point_types.h"
41#include "pcl/PointIndices.h"
42#include "sensor_msgs/msg/laser_scan.hpp"
43#include "sensor_msgs/msg/point_cloud2.hpp"
44#include "rclcpp/time.hpp"
45#include "rclcpp_lifecycle/lifecycle_node.hpp"
55struct hash<
std::tuple<int, int, int>>
60 std::size_t
operator()(
const std::tuple<int, int, int> & key)
const
62 std::size_t h1 = std::hash<int>()(std::get<0>(key));
63 std::size_t h2 = std::hash<int>()(std::get<1>(key));
64 std::size_t h3 = std::hash<int>()(std::get<2>(key));
65 return h1 ^ (h2 << 1) ^ (h3 << 2);
76 pcl::PointCloud<pcl::PointXYZ>
data;
98 return t ==
"sensor_msgs/msg/LaserScan" ||
99 t ==
"sensor_msgs/msg/PointCloud2";
104 [[maybe_unused]]
static const bool _ = [] {
107 std::ostringstream ret;
108 ret <<
"{ " << perception.
stamp.seconds()
109 <<
" } PointPerception with " << perception.
data.size()
110 <<
" points in frame [" << perception.
frame_id
111 <<
"] with ts " << perception.
stamp.seconds() <<
"\n";
120 std::lock_guard<std::mutex> lock(other.
mutex_);
136 if (
this == &other) {
157 pcl::PointCloud<pcl::PointXYZ>
data;
166 pcl::PointCloud<pcl::PointXYZ> && cloud,
167 std::string && frame,
168 const rclcpp::Time &
stamp)
170 std::lock_guard<std::mutex> lock(
mutex_);
182 data.points.resize(size);
187 std::lock_guard<std::mutex> lock(
mutex_);
190 auto & tf_buffer = *tf_buffer_ptr;
191 const auto tf_info = tf_buffer.get_tf_info();
192 const std::string & robot_frame = tf_info.robot_frame;
203 buffer.push(std::move(pending_item));
207 const std::size_t count =
buffer.size();
217 std::vector<PointPerceptionBufferType> items;
218 items.reserve(count);
220 for (std::size_t i = 0; i < count; ++i) {
225 items.push_back(std::move(item));
241 std::optional<std::size_t> newest_idx;
242 std::optional<std::size_t> newest_valid_idx;
244 for (std::size_t i = 0; i < items.size(); ++i) {
245 const auto & item = items[i];
248 if (!newest_idx || item.stamp > items[*newest_idx].stamp) {
254 has_tf = tf_buffer.canTransform(
257 tf2_ros::fromMsg(item.stamp));
264 if (!newest_valid_idx || item.stamp > items[*newest_valid_idx].stamp) {
265 newest_valid_idx = i;
276 if (newest_valid_idx) {
277 const auto & sel = items[*newest_valid_idx];
283 }
else if (newest_idx) {
284 const auto & sel = items[*newest_idx];
300 if (newest_valid_idx) {
302 const rclcpp::Time cutoff_stamp = items[*newest_valid_idx].stamp;
304 for (
auto & item : items) {
305 if (item.stamp >= cutoff_stamp) {
306 buffer.push(std::move(item));
311 for (
auto & item : items) {
312 buffer.push(std::move(item));
342 bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state)
override;
345 std::shared_ptr<PerceptionBase>
get_perception()
const override {
return perception_data_;}
349 std::shared_ptr<PointPerception> perception_data_ {
nullptr};
352 rclcpp::SubscriptionBase::SharedPtr perception_sub_;
358void convert(
const sensor_msgs::msg::LaserScan & scan, pcl::PointCloud<pcl::PointXYZ> & pc);
368sensor_msgs::msg::PointCloud2
points_to_rosmsg(
const pcl::PointCloud<pcl::PointXYZ> & points);
373 std::vector<std::shared_ptr<PointPerception>>;
404 return x == other.
x &&
y == other.
y &&
z == other.
z;
417 std::size_t h1 = std::hash<int>{}(key.
x);
418 std::size_t h2 = std::hash<int>{}(key.
y);
419 std::size_t h3 = std::hash<int>{}(key.
z);
420 return h1 ^ (h2 << 1) ^ (h3 << 2);
470 const std::vector<double> & min_bounds,
471 const std::vector<double> & max_bounds,
472 bool lazy_post_fuse =
true);
497 collapse(
const std::vector<double> & collapse_dims,
bool lazy =
true);
501 pcl::PointCloud<pcl::PointXYZ>
as_points()
const;
534 const std::string & target_frame,
535 rclcpp::Time & stamp,
536 bool exact_time =
false);
554 const pcl::PointCloud<pcl::PointXYZ> points,
555 const std::string & frame,
567 std::optional<PointPerceptions> owned_;
569 std::vector<pcl::PointIndices> indices_;
573 bool has_target_frame_ {
false};
575 std::string target_frame_;
577 std::vector<tf2::Transform> tf_transforms_;
579 std::vector<bool> tf_valid_;
582 bool collapse_x_ {
false};
583 bool collapse_y_ {
false};
584 bool collapse_z_ {
false};
585 float collapse_val_x_ {0.0f};
586 float collapse_val_y_ {0.0f};
587 float collapse_val_z_ {0.0f};
589 bool has_post_filter_ {
false};
590 double post_min_[3] {0.0, 0.0, 0.0};
591 double post_max_[3] {0.0, 0.0, 0.0};
592 bool use_post_min_[3] {
false,
false,
false};
593 bool use_post_max_[3] {
false,
false,
false};
Defines data structures and utilities for representing and processing sensor perceptions.
Fixed-size circular buffer, thread-safe (mutex-based), copyable.
Definition CircularBuffer.hpp:34
static void register_printer(std::function< std::string(const T &)> printer)
Registers a pretty-printer for type T used by debug_string().
Definition NavState.hpp:458
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:48
bool valid
Whether the perception holds usable data: set with new data, cleared by SensorsNode once it is older ...
Definition Perceptions.hpp:60
rclcpp::Time stamp
Timestamp of the perception (ROS time).
Definition Perceptions.hpp:53
bool new_data
Whether the data has changed since the last observation.
Definition Perceptions.hpp:63
std::string frame_id
Coordinate frame associated with the perception.
Definition Perceptions.hpp:56
Abstract base class for pluginlib-based sensor perception handlers.
Definition Perceptions.hpp:117
PerceptionHandler implementation for sensors producing point-based data.
Definition PointPerception.hpp:328
bool cycle_rt(std::shared_ptr< NavState > nav_state) override
Run one real-time sensor processing cycle.
Definition PointPerception.cpp:143
void on_initialize() override
Optional post-initialization hook for subclasses.
Definition PointPerception.cpp:88
std::shared_ptr< PerceptionBase > get_perception() const override
The perception this handler keeps up to date.
Definition PointPerception.hpp:345
Concrete perception class for 3D point cloud data.
Definition PointPerception.hpp:87
pcl::PointCloud< pcl::PointXYZ > pending_cloud_
Definition PointPerception.hpp:160
std::mutex mutex_
Definition PointPerception.hpp:318
static bool supports_msg_type(std::string_view t)
Checks if a ROS message type is supported by this perception.
Definition PointPerception.hpp:96
PointPerception()
Definition PointPerception.hpp:102
void set_pending_cloud(pcl::PointCloud< pcl::PointXYZ > &&cloud, std::string &&frame, const rclcpp::Time &stamp)
Stores a pending cloud atomically for later integration.
Definition PointPerception.hpp:165
CircularBuffer< PointPerceptionBufferType > buffer
Definition PointPerception.hpp:319
static constexpr std::string_view default_group_
Group identifier for point perceptions.
Definition PointPerception.hpp:90
std::string pending_frame_
Definition PointPerception.hpp:161
bool pending_available_
Definition PointPerception.hpp:159
pcl::PointCloud< pcl::PointXYZ > data
The 3D point cloud data associated with this perception.
Definition PointPerception.hpp:157
PointPerception & operator=(const PointPerception &other)
Definition PointPerception.hpp:134
rclcpp::Time pending_stamp_
Definition PointPerception.hpp:162
PointPerception(const PointPerception &other)
Definition PointPerception.hpp:118
void resize(std::size_t size)
Resizes the internal point cloud storage.
Definition PointPerception.hpp:180
void integrate_pending_perceptions()
Definition PointPerception.hpp:185
PointPerceptionsOpsView & downsample(double resolution)
Downsamples each perception using a voxel grid.
Definition PointPerception.cpp:366
PointPerceptionsOpsView & add(const pcl::PointCloud< pcl::PointXYZ > points, const std::string &frame, rclcpp::Time stamp)
Adds a new perception to the current view.
Definition PointPerception.cpp:697
PointPerceptionsOpsView & fuse(const std::string &target_frame, bool exact_time=false)
Configures fusion of all perceptions into a common frame.
Definition PointPerception.cpp:551
PointPerceptionsOpsView(PointPerceptionsOpsView &&)=default
const PointPerceptions & get_perceptions() const
Provides a constant reference to the underlying perceptions container.
Definition PointPerception.hpp:560
pcl::PointCloud< pcl::PointXYZ > as_points() const
Retrieves all selected points across perceptions as a single concatenated cloud.
Definition PointPerception.cpp:478
PointPerceptionsOpsView(const PointPerceptions &perceptions)
Constructs a view over an external container of perceptions.
Definition PointPerception.cpp:204
rclcpp::Time get_latest_stamp() const
Retrieves the latest timestamp across all perceptions.
Definition PointPerception.cpp:753
PointPerceptionsOpsView & collapse(const std::vector< double > &collapse_dims, bool lazy=true)
Collapses dimensions to fixed values (for example, projection onto a plane).
Definition PointPerception.cpp:417
PointPerceptionsOpsView(const PointPerceptionsOpsView &)=delete
PointPerceptionsOpsView & operator=(const PointPerceptionsOpsView &)=delete
PointPerceptionsOpsView & filter(const std::vector< double > &min_bounds, const std::vector< double > &max_bounds, bool lazy_post_fuse=true)
Filters all point clouds by axis-aligned bounds.
Definition PointPerception.cpp:264
static RTTFBuffer * getInstance(Args &&... args)
Definition Singleton.hpp:30
Definition CircularBuffer.hpp:24
rclcpp::Time get_latest_point_perceptions_stamp(const PointPerceptions &perceptions)
Retrieves the latest timestamp among a set of point-based perceptions.
Definition PointPerception.cpp:730
sensor_msgs::msg::PointCloud2 points_to_rosmsg(const pcl::PointCloud< pcl::PointXYZ > &points)
Converts a PCL point cloud into a sensor_msgs::msg::PointCloud2 message.
Definition PointPerception.cpp:196
sensor_msgs::msg::PointCloud2 perception_to_rosmsg(const PointPerception &perception)
Converts a PointPerception into a sensor_msgs::msg::PointCloud2 message.
Definition PointPerception.cpp:186
void convert(const sensor_msgs::msg::LaserScan &scan, pcl::PointCloud< pcl::PointXYZ > &pc)
Converts a LaserScan message into a point cloud.
Definition PointPerception.cpp:154
std::vector< std::shared_ptr< PointPerception > > PointPerceptions
Alias for a vector of shared pointers to PointPerception objects.
Definition PointPerception.hpp:372
Definition PointPerception.hpp:51
Definition PointPerception.hpp:75
rclcpp::Time stamp
Definition PointPerception.hpp:78
pcl::PointCloud< pcl::PointXYZ > data
Definition PointPerception.hpp:76
std::string frame
Definition PointPerception.hpp:77
Hash functor for VoxelKey.
Definition PointPerception.hpp:411
std::size_t operator()(const VoxelKey &key) const
Computes the hash value.
Definition PointPerception.hpp:415
Discrete 3D voxel index used for downsampling.
Definition PointPerception.hpp:395
int y
Definition PointPerception.hpp:397
int z
Definition PointPerception.hpp:397
int x
Discrete coordinates (voxel indices).
Definition PointPerception.hpp:397
bool operator==(const VoxelKey &other) const
Equality comparison.
Definition PointPerception.hpp:402
std::size_t operator()(const std::tuple< int, int, int > &key) const
Computes the hash value.
Definition PointPerception.hpp:60