26#ifndef EASYNAV_SENSORS_TYPES__POINTPERCEPTIONS_HPP_
27#define EASYNAV_SENSORS_TYPES__POINTPERCEPTIONS_HPP_
34#include "tf2/LinearMath/Transform.hpp"
35#include "pcl/point_cloud.h"
36#include "pcl/point_types.h"
37#include "pcl/PointIndices.h"
39#include "sensor_msgs/msg/laser_scan.hpp"
40#include "sensor_msgs/msg/point_cloud2.hpp"
42#include "rclcpp/time.hpp"
43#include "rclcpp_lifecycle/lifecycle_node.hpp"
54struct hash<
std::tuple<int, int, int>>
59 std::size_t
operator()(
const std::tuple<int, int, int> & key)
const
61 std::size_t h1 = std::hash<int>()(std::get<0>(key));
62 std::size_t h2 = std::hash<int>()(std::get<1>(key));
63 std::size_t h3 = std::hash<int>()(std::get<2>(key));
64 return h1 ^ (h2 << 1) ^ (h3 << 2);
75 pcl::PointCloud<pcl::PointXYZ>
data;
96 return t ==
"sensor_msgs/msg/LaserScan" ||
97 t ==
"sensor_msgs/msg/PointCloud2";
102 [[maybe_unused]]
static const bool _ = [] {
105 std::ostringstream ret;
106 ret <<
"{ " << perception.
stamp.seconds()
107 <<
" } PointPerception with " << perception.
data.size()
108 <<
" points in frame [" << perception.
frame_id
109 <<
"] with ts " << perception.
stamp.seconds() <<
"\n";
118 std::lock_guard<std::mutex> lock(other.
mutex_);
134 if (
this == &other) {
155 pcl::PointCloud<pcl::PointXYZ>
data;
164 pcl::PointCloud<pcl::PointXYZ> && cloud,
165 std::string && frame,
166 const rclcpp::Time &
stamp)
168 std::lock_guard<std::mutex> lock(
mutex_);
180 data.points.resize(size);
186 return buffer.latest_ref();
191 std::lock_guard<std::mutex> lock(
mutex_);
194 auto & tf_buffer = *tf_buffer_ptr;
195 const auto tf_info = tf_buffer.get_tf_info();
196 const std::string & robot_frame = tf_info.robot_frame;
207 buffer.push(std::move(pending_item));
211 const std::size_t count =
buffer.size();
221 std::vector<PointPerceptionBufferType> items;
222 items.reserve(count);
224 for (std::size_t i = 0; i < count; ++i) {
229 items.push_back(std::move(item));
245 std::optional<std::size_t> newest_idx;
246 std::optional<std::size_t> newest_valid_idx;
248 for (std::size_t i = 0; i < items.size(); ++i) {
249 const auto & item = items[i];
252 if (!newest_idx || item.stamp > items[*newest_idx].stamp) {
258 has_tf = tf_buffer.canTransform(
261 tf2_ros::fromMsg(item.stamp),
262 tf2::durationFromSec(0.0));
269 if (!newest_valid_idx || item.stamp > items[*newest_valid_idx].stamp) {
270 newest_valid_idx = i;
281 if (newest_valid_idx) {
282 const auto & sel = items[*newest_valid_idx];
288 }
else if (newest_idx) {
289 const auto & sel = items[*newest_idx];
305 if (newest_valid_idx) {
307 const rclcpp::Time cutoff_stamp = items[*newest_valid_idx].stamp;
309 for (
auto & item : items) {
310 if (item.stamp >= cutoff_stamp) {
311 buffer.push(std::move(item));
316 for (
auto & item : items) {
317 buffer.push(std::move(item));
347 bool cycle_rt([[maybe_unused]] std::shared_ptr<NavState> nav_state)
override;
351 std::shared_ptr<PointPerception> perception_data_ {
nullptr};
354 rclcpp::SubscriptionBase::SharedPtr perception_sub_;
360void convert(
const sensor_msgs::msg::LaserScan & scan, pcl::PointCloud<pcl::PointXYZ> & pc);
370sensor_msgs::msg::PointCloud2
points_to_rosmsg(
const pcl::PointCloud<pcl::PointXYZ> & points);
375 std::vector<std::shared_ptr<PointPerception>>;
403 return x == other.
x &&
y == other.
y &&
z == other.
z;
416 std::size_t h1 = std::hash<int>{}(key.
x);
417 std::size_t h2 = std::hash<int>{}(key.
y);
418 std::size_t h3 = std::hash<int>{}(key.
z);
419 return h1 ^ (h2 << 1) ^ (h3 << 2);
469 const std::vector<double> & min_bounds,
470 const std::vector<double> & max_bounds,
471 bool lazy_post_fuse =
true);
496 collapse(
const std::vector<double> & collapse_dims,
bool lazy =
true);
500 pcl::PointCloud<pcl::PointXYZ>
as_points()
const;
505 const pcl::PointCloud<pcl::PointXYZ> &
as_points(
int idx)
const;
536 const std::string & target_frame,
537 rclcpp::Time & stamp,
538 bool exact_time =
false);
556 const pcl::PointCloud<pcl::PointXYZ> points,
557 const std::string & frame,
569 std::optional<PointPerceptions> owned_;
571 std::vector<pcl::PointIndices> indices_;
574 bool has_target_frame_ {
false};
575 std::string target_frame_;
576 std::vector<tf2::Transform> tf_transforms_;
577 std::vector<bool> tf_valid_;
580 bool collapse_x_ {
false};
581 bool collapse_y_ {
false};
582 bool collapse_z_ {
false};
583 float collapse_val_x_ {0.0f};
584 float collapse_val_y_ {0.0f};
585 float collapse_val_z_ {0.0f};
587 bool has_post_filter_ {
false};
588 double post_min_[3] {0.0, 0.0, 0.0};
589 double post_max_[3] {0.0, 0.0, 0.0};
590 bool use_post_min_[3] {
false,
false,
false};
591 bool use_post_max_[3] {
false,
false,
false};
594 mutable pcl::PointCloud<pcl::PointXYZ> tmp_single_cloud_;
Defines data structures and utilities for representing and processing sensor perceptions.
Fixed-size circular buffer, thread-safe (mutex-based), copyable.
Definition CircularBuffer.hpp:33
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:384
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:44
bool valid
Whether the perception contains valid data.
Definition Perceptions.hpp:56
rclcpp::Time stamp
Timestamp of the perception (ROS time).
Definition Perceptions.hpp:49
bool new_data
Whether the data has changed since the last observation.
Definition Perceptions.hpp:59
std::string frame_id
Coordinate frame associated with the perception.
Definition Perceptions.hpp:52
Abstract base class for pluginlib-based sensor perception handlers.
Definition Perceptions.hpp:108
PerceptionHandler implementation for sensors producing point-based data.
Definition PointPerception.hpp:333
bool cycle_rt(std::shared_ptr< NavState > nav_state) override
Run one real-time sensor processing cycle.
Definition PointPerception.cpp:152
void on_initialize() override
Optional post-initialization hook for subclasses.
Definition PointPerception.cpp:92
Concrete perception class for 3D point cloud data.
Definition PointPerception.hpp:86
pcl::PointCloud< pcl::PointXYZ > pending_cloud_
Definition PointPerception.hpp:158
std::mutex mutex_
Definition PointPerception.hpp:323
static bool supports_msg_type(std::string_view t)
Checks if a ROS message type is supported by this perception.
Definition PointPerception.hpp:94
PointPerception()
Definition PointPerception.hpp:100
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:163
CircularBuffer< PointPerceptionBufferType > buffer
Definition PointPerception.hpp:324
static constexpr std::string_view default_group_
Group identifier for point perceptions.
Definition PointPerception.hpp:89
std::string pending_frame_
Definition PointPerception.hpp:159
bool pending_available_
Definition PointPerception.hpp:157
pcl::PointCloud< pcl::PointXYZ > data
The 3D point cloud data associated with this perception.
Definition PointPerception.hpp:155
const PointPerceptionBufferType & get_last_perception() const
Retrieves the most recent buffered perception (independently of it has a valid TF) without removing i...
Definition PointPerception.hpp:184
PointPerception & operator=(const PointPerception &other)
Definition PointPerception.hpp:132
rclcpp::Time pending_stamp_
Definition PointPerception.hpp:160
PointPerception(const PointPerception &other)
Definition PointPerception.hpp:116
void resize(std::size_t size)
Resizes the internal point cloud storage.
Definition PointPerception.hpp:178
void integrate_pending_perceptions()
Definition PointPerception.hpp:189
PointPerceptionsOpsView & downsample(double resolution)
Downsamples each perception using a voxel grid.
Definition PointPerception.cpp:377
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:763
PointPerceptionsOpsView & fuse(const std::string &target_frame, bool exact_time=false)
Configures fusion of all perceptions into a common frame.
Definition PointPerception.cpp:619
PointPerceptionsOpsView(PointPerceptionsOpsView &&)=default
const PointPerceptions & get_perceptions() const
Provides a constant reference to the underlying perceptions container.
Definition PointPerception.hpp:562
pcl::PointCloud< pcl::PointXYZ > as_points() const
Retrieves all selected points across perceptions as a single concatenated cloud.
Definition PointPerception.cpp:489
PointPerceptionsOpsView(const PointPerceptions &perceptions)
Constructs a view over an external container of perceptions.
Definition PointPerception.cpp:213
rclcpp::Time get_latest_stamp() const
Retrieves the latest timestamp across all perceptions.
Definition PointPerception.cpp:819
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:428
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:273
static RTTFBuffer * getInstance(Args &&... args)
Definition Singleton.hpp:31
Definition CircularBuffer.hpp:23
rclcpp::Time get_latest_point_perceptions_stamp(const PointPerceptions &perceptions)
Retrieves the latest timestamp among a set of point-based perceptions.
Definition PointPerception.cpp:796
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:205
sensor_msgs::msg::PointCloud2 perception_to_rosmsg(const PointPerception &perception)
Converts a PointPerception into a sensor_msgs::msg::PointCloud2 message.
Definition PointPerception.cpp:195
void convert(const sensor_msgs::msg::LaserScan &scan, pcl::PointCloud< pcl::PointXYZ > &pc)
Converts a LaserScan message into a point cloud.
Definition PointPerception.cpp:163
std::vector< std::shared_ptr< PointPerception > > PointPerceptions
Alias for a vector of shared pointers to PointPerception objects.
Definition PointPerception.hpp:374
Definition PointPerception.hpp:50
Definition PointPerception.hpp:74
rclcpp::Time stamp
Definition PointPerception.hpp:77
pcl::PointCloud< pcl::PointXYZ > data
Definition PointPerception.hpp:75
std::string frame
Definition PointPerception.hpp:76
Hash functor for VoxelKey.
Definition PointPerception.hpp:410
std::size_t operator()(const VoxelKey &key) const
Computes the hash value.
Definition PointPerception.hpp:414
Discrete 3D voxel index used for downsampling.
Definition PointPerception.hpp:394
int y
Definition PointPerception.hpp:396
int z
Definition PointPerception.hpp:396
int x
Discrete coordinates (voxel indices).
Definition PointPerception.hpp:396
bool operator==(const VoxelKey &other) const
Equality comparison.
Definition PointPerception.hpp:401
std::size_t operator()(const std::tuple< int, int, int > &key) const
Computes the hash value.
Definition PointPerception.hpp:59