31#ifndef EASYNAV_COMMON_TYPES__POINTPERCEPTIONS_HPP_
32#define EASYNAV_COMMON_TYPES__POINTPERCEPTIONS_HPP_
38#include "tf2/LinearMath/Transform.hpp"
39#include "pcl/point_cloud.h"
40#include "pcl/point_types.h"
41#include "pcl/PointIndices.h"
43#include "sensor_msgs/msg/laser_scan.hpp"
44#include "sensor_msgs/msg/point_cloud2.hpp"
46#include "rclcpp/time.hpp"
47#include "rclcpp_lifecycle/lifecycle_node.hpp"
58struct hash<
std::tuple<int, int, int>>
63 std::size_t
operator()(
const std::tuple<int, int, int> & key)
const
65 std::size_t h1 = std::hash<int>()(std::get<0>(key));
66 std::size_t h2 = std::hash<int>()(std::get<1>(key));
67 std::size_t h3 = std::hash<int>()(std::get<2>(key));
68 return h1 ^ (h2 << 1) ^ (h3 << 2);
79 pcl::PointCloud<pcl::PointXYZ>
data;
100 return t ==
"sensor_msgs/msg/LaserScan" ||
101 t ==
"sensor_msgs/msg/PointCloud2";
105 pcl::PointCloud<pcl::PointXYZ>
data;
117 data.points.resize(size);
123 return buffer.latest_ref();
130 auto & tf_buffer = *tf_buffer_ptr;
131 const auto tf_info = tf_buffer.get_tf_info();
132 const std::string & robot_frame = tf_info.robot_frame;
143 buffer.push(std::move(pending_item));
147 const std::size_t count =
buffer.size();
157 std::vector<PointPerceptionBufferType> items;
158 items.reserve(count);
160 for (std::size_t i = 0; i < count; ++i) {
165 items.push_back(std::move(item));
181 std::optional<std::size_t> newest_idx;
182 std::optional<std::size_t> newest_valid_idx;
184 for (std::size_t i = 0; i < items.size(); ++i) {
185 const auto & item = items[i];
188 if (!newest_idx || item.stamp > items[*newest_idx].stamp) {
194 has_tf = tf_buffer.canTransform(
197 tf2_ros::fromMsg(item.stamp),
198 tf2::durationFromSec(0.0));
205 if (!newest_valid_idx || item.stamp > items[*newest_valid_idx].stamp) {
206 newest_valid_idx = i;
217 if (newest_valid_idx) {
218 const auto & sel = items[*newest_valid_idx];
224 }
else if (newest_idx) {
225 const auto & sel = items[*newest_idx];
241 if (newest_valid_idx) {
243 const rclcpp::Time cutoff_stamp = items[*newest_valid_idx].stamp;
245 for (
auto & item : items) {
246 if (item.stamp >= cutoff_stamp) {
247 buffer.push(std::move(item));
252 for (
auto & item : items) {
253 buffer.push(std::move(item));
272 std::string
group()
const override {
return "points";}
277 std::shared_ptr<PerceptionBase>
create(
const std::string &)
override
279 return std::make_shared<PointPerception>();
295 rclcpp_lifecycle::LifecycleNode & node,
296 const std::string & topic,
297 const std::string & type,
298 std::shared_ptr<PerceptionBase> target,
299 rclcpp::CallbackGroup::SharedPtr cb_group)
override;
305void convert(
const sensor_msgs::msg::LaserScan & scan, pcl::PointCloud<pcl::PointXYZ> & pc);
315sensor_msgs::msg::PointCloud2
points_to_rosmsg(
const pcl::PointCloud<pcl::PointXYZ> & points);
320 std::vector<std::shared_ptr<PointPerception>>;
353 return x == other.
x &&
y == other.
y &&
z == other.
z;
366 std::size_t h1 = std::hash<int>{}(key.
x);
367 std::size_t h2 = std::hash<int>{}(key.
y);
368 std::size_t h3 = std::hash<int>{}(key.
z);
369 return h1 ^ (h2 << 1) ^ (h3 << 2);
419 const std::vector<double> & min_bounds,
420 const std::vector<double> & max_bounds,
421 bool lazy_post_fuse =
true);
446 collapse(
const std::vector<double> & collapse_dims,
bool lazy =
true);
450 pcl::PointCloud<pcl::PointXYZ>
as_points()
const;
455 const pcl::PointCloud<pcl::PointXYZ> &
as_points(
int idx)
const;
486 const std::string & target_frame,
487 rclcpp::Time & stamp,
488 bool exact_time =
true);
506 const pcl::PointCloud<pcl::PointXYZ> points,
507 const std::string & frame,
519 std::optional<PointPerceptions> owned_;
521 std::vector<pcl::PointIndices> indices_;
524 bool has_target_frame_ {
false};
525 std::string target_frame_;
526 std::vector<tf2::Transform> tf_transforms_;
527 std::vector<bool> tf_valid_;
530 bool collapse_x_ {
false};
531 bool collapse_y_ {
false};
532 bool collapse_z_ {
false};
533 float collapse_val_x_ {0.0f};
534 float collapse_val_y_ {0.0f};
535 float collapse_val_z_ {0.0f};
537 bool has_post_filter_ {
false};
538 double post_min_[3] {0.0, 0.0, 0.0};
539 double post_max_[3] {0.0, 0.0, 0.0};
540 bool use_post_min_[3] {
false,
false,
false};
541 bool use_post_max_[3] {
false,
false,
false};
544 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:38
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:45
bool valid
Whether the perception contains valid data.
Definition Perceptions.hpp:56
rclcpp::Time stamp
Timestamp of the perception (ROS time).
Definition Perceptions.hpp:50
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:53
Abstract base interface for group-specific perception handlers.
Definition Perceptions.hpp:121
PerceptionHandler implementation for sensors producing point-based data.
Definition PointPerception.hpp:268
std::shared_ptr< PerceptionBase > create(const std::string &) override
Creates a new PointPerception instance.
Definition PointPerception.hpp:277
std::string group() const override
Returns the sensor group handled by this handler.
Definition PointPerception.hpp:272
rclcpp::SubscriptionBase::SharedPtr create_subscription(rclcpp_lifecycle::LifecycleNode &node, const std::string &topic, const std::string &type, std::shared_ptr< PerceptionBase > target, rclcpp::CallbackGroup::SharedPtr cb_group) override
Creates a subscription to LaserScan or PointCloud2 messages and updates the perception.
Definition PointPerception.cpp:97
Concrete perception class for 3D point cloud data.
Definition PointPerception.hpp:90
pcl::PointCloud< pcl::PointXYZ > pending_cloud_
Definition PointPerception.hpp:108
static bool supports_msg_type(std::string_view t)
Checks if a ROS message type is supported by this perception.
Definition PointPerception.hpp:98
CircularBuffer< PointPerceptionBufferType > buffer
Definition PointPerception.hpp:259
static constexpr std::string_view default_group_
Group identifier for point perceptions.
Definition PointPerception.hpp:93
std::string pending_frame_
Definition PointPerception.hpp:109
bool pending_available_
Definition PointPerception.hpp:107
pcl::PointCloud< pcl::PointXYZ > data
The 3D point cloud data associated with this perception.
Definition PointPerception.hpp:105
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:121
rclcpp::Time pending_stamp_
Definition PointPerception.hpp:110
void resize(std::size_t size)
Resizes the internal point cloud storage.
Definition PointPerception.hpp:115
void integrate_pending_perceptions()
Definition PointPerception.hpp:126
PointPerceptionsOpsView & downsample(double resolution)
Downsamples each perception using a voxel grid.
Definition PointPerception.cpp:361
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:671
PointPerceptionsOpsView & fuse(const std::string &target_frame, bool exact_time=true)
Configures fusion of all perceptions into a common frame.
Definition PointPerception.cpp:603
PointPerceptionsOpsView(PointPerceptionsOpsView &&)=default
const PointPerceptions & get_perceptions() const
Provides a constant reference to the underlying perceptions container.
Definition PointPerception.hpp:512
pcl::PointCloud< pcl::PointXYZ > as_points() const
Retrieves all selected points across perceptions as a single concatenated cloud.
Definition PointPerception.cpp:473
PointPerceptionsOpsView(const PointPerceptions &perceptions)
Constructs a view over an external container of perceptions.
Definition PointPerception.cpp:197
rclcpp::Time get_latest_stamp() const
Retrieves the latest timestamp across all perceptions.
Definition PointPerception.cpp:725
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:412
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:257
static RTTFBuffer * getInstance(Args &&... args)
Definition Singleton.hpp:36
Definition CircularBuffer.hpp:28
rclcpp::Time get_latest_point_perceptions_stamp(const PointPerceptions &perceptions)
Retrieves the latest timestamp among a set of point-based perceptions.
Definition PointPerception.cpp:710
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:189
sensor_msgs::msg::PointCloud2 perception_to_rosmsg(const PointPerception &perception)
Converts a PointPerception into a sensor_msgs::msg::PointCloud2 message.
Definition PointPerception.cpp:179
void convert(const sensor_msgs::msg::LaserScan &scan, pcl::PointCloud< pcl::PointXYZ > &pc)
Converts a LaserScan message into a point cloud.
Definition PointPerception.cpp:147
std::vector< std::shared_ptr< PointPerception > > PointPerceptions
Alias for a vector of shared pointers to PointPerception objects.
Definition PointPerception.hpp:319
PointPerceptions get_point_perceptions(std::vector< PerceptionPtr > &perceptionptr)
Extracts all PointPerception objects from a heterogeneous collection.
Definition PointPerception.cpp:705
Definition PointPerception.hpp:54
Definition PointPerception.hpp:78
rclcpp::Time stamp
Definition PointPerception.hpp:81
pcl::PointCloud< pcl::PointXYZ > data
Definition PointPerception.hpp:79
std::string frame
Definition PointPerception.hpp:80
Hash functor for VoxelKey.
Definition PointPerception.hpp:360
std::size_t operator()(const VoxelKey &key) const
Computes the hash value.
Definition PointPerception.hpp:364
Discrete 3D voxel index used for downsampling.
Definition PointPerception.hpp:344
int y
Definition PointPerception.hpp:346
int z
Definition PointPerception.hpp:346
int x
Discrete coordinates (voxel indices).
Definition PointPerception.hpp:346
bool operator==(const VoxelKey &other) const
Equality comparison.
Definition PointPerception.hpp:351
std::size_t operator()(const std::tuple< int, int, int > &key) const
Computes the hash value.
Definition PointPerception.hpp:63