Easy Navigation
Loading...
Searching...
No Matches
PointPerception.hpp
Go to the documentation of this file.
1// Copyright 2025 Intelligent Robotics Lab
2//
3// This file is part of the project Easy Navigation (EasyNav in short)
4// licensed under the GNU General Public License v3.0.
5// See <http://www.gnu.org/licenses/> for details.
6//
7// Easy Navigation program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
23
32
33#ifndef EASYNAV_COMMON_TYPES__POINTPERCEPTIONS_HPP_
34#define EASYNAV_COMMON_TYPES__POINTPERCEPTIONS_HPP_
35
36#include <string>
37#include <vector>
38#include <optional>
39
40#include "pcl_conversions/pcl_conversions.h"
41#include "pcl/point_types_conversion.h"
42#include "pcl/common/transforms.h"
43#include "pcl/point_cloud.h"
44#include "pcl/point_types.h"
45#include "pcl/PointIndices.h"
46
47#include "sensor_msgs/msg/laser_scan.hpp"
48#include "sensor_msgs/msg/point_cloud2.hpp"
49
50#include "rclcpp/time.hpp"
51#include "rclcpp_lifecycle/lifecycle_node.hpp"
52
54
55namespace std
56{
57
59template<>
60struct hash<std::tuple<int, int, int>>
61{
62 std::size_t operator()(const std::tuple<int, int, int> & key) const
63 {
64 std::size_t h1 = std::hash<int>()(std::get<0>(key));
65 std::size_t h2 = std::hash<int>()(std::get<1>(key));
66 std::size_t h3 = std::hash<int>()(std::get<2>(key));
67 return h1 ^ (h2 << 1) ^ (h3 << 2);
68 }
69};
70
71} // namespace std
72
73namespace easynav
74{
75
82{
83public:
85 pcl::PointCloud<pcl::PointXYZ> data;
86
89 void resize(std::size_t size)
90 {
91 data.points.resize(size);
92 }
93};
94
101{
102public:
104 std::string group() const override {return "points";}
105
109 std::shared_ptr<PerceptionBase> create(const std::string &) override
110 {
111 return std::make_shared<PointPerception>();
112 }
113
115 rclcpp::SubscriptionBase::SharedPtr create_subscription(
116 rclcpp_lifecycle::LifecycleNode & node,
117 const std::string & topic,
118 const std::string & type,
119 std::shared_ptr<PerceptionBase> target,
120 rclcpp::CallbackGroup::SharedPtr cb_group) override;
121};
122
126void convert(const sensor_msgs::msg::LaserScan & scan, pcl::PointCloud<pcl::PointXYZ> & pc);
127
131sensor_msgs::msg::PointCloud2 perception_to_rosmsg(const PointPerception & perception);
132
136sensor_msgs::msg::PointCloud2 points_to_rosmsg(const pcl::PointCloud<pcl::PointXYZ> & points);
137
141 std::vector<std::shared_ptr<PointPerception>>;
142
143PointPerceptions get_point_perceptions(std::vector<PerceptionPtr> & perceptionptr);
144
151{
152public:
155 struct VoxelKey
156 {
157 int x, y, z;
158
160 bool operator==(const VoxelKey & other) const
161 {
162 return x == other.x && y == other.y && z == other.z;
163 }
164 };
165
169 {
170 std::size_t operator()(const VoxelKey & key) const
171 {
172 std::size_t h1 = std::hash<int>{}(key.x);
173 std::size_t h2 = std::hash<int>{}(key.y);
174 std::size_t h3 = std::hash<int>{}(key.z);
175 return h1 ^ (h2 << 1) ^ (h3 << 2);
176 }
177 };
178
181 explicit PointPerceptionsOpsView(const PointPerceptions & perceptions);
182
185 explicit PointPerceptionsOpsView(PointPerceptions && perceptions);
186
192 const std::vector<double> & min_bounds,
193 const std::vector<double> & max_bounds);
194
198 PointPerceptionsOpsView & downsample(double resolution);
199
203 std::shared_ptr<PointPerceptionsOpsView> collapse(
204 const std::vector<double> & collapse_dims) const;
205
208 pcl::PointCloud<pcl::PointXYZ> as_points() const;
209
213 const pcl::PointCloud<pcl::PointXYZ> & as_points(int idx) const;
214
218 std::shared_ptr<PointPerceptionsOpsView> fuse(const std::string & target_frame) const;
219
222 const PointPerceptions & get_perceptions() const {return perceptions_;}
223
224private:
225 std::optional<PointPerceptions> owned_;
226 const PointPerceptions & perceptions_;
227 std::vector<pcl::PointIndices> indices_;
228};
229
230} // namespace easynav
231
232#endif // EASYNAV_COMMON_TYPES__POINTPERCEPTIONS_HPP_
Defines data structures and utilities for representing and processing sensor perceptions.
Abstract base class for representing a single sensor perception.
Definition Perceptions.hpp:50
Abstract base interface for group-specific perception handlers.
Definition Perceptions.hpp:87
PerceptionHandler implementation for sensors producing point-based data.
Definition PointPerception.hpp:101
std::shared_ptr< PerceptionBase > create(const std::string &) override
Creates a new PointPerception instance.
Definition PointPerception.hpp:109
std::string group() const override
Returns the sensor group handled by this handler: "points".
Definition PointPerception.hpp:104
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:48
Concrete perception class for 3D point cloud data.
Definition PointPerception.hpp:82
pcl::PointCloud< pcl::PointXYZ > data
The 3D point cloud data associated with this perception.
Definition PointPerception.hpp:85
void resize(std::size_t size)
Resize the internal point cloud to a fixed number of points.
Definition PointPerception.hpp:89
Provides efficient, non-destructive, chainable operations over a set of point-based perceptions.
Definition PointPerception.hpp:151
PointPerceptionsOpsView & downsample(double resolution)
Downsamples each perception using a voxel grid.
Definition PointPerception.cpp:204
const PointPerceptions & get_perceptions() const
Gets a reference to the underlying perceptions container.
Definition PointPerception.hpp:222
pcl::PointCloud< pcl::PointXYZ > as_points() const
Retrieves all selected points as a single cloud.
Definition PointPerception.cpp:264
PointPerceptionsOpsView & filter(const std::vector< double > &min_bounds, const std::vector< double > &max_bounds)
Filters all point clouds by spatial bounds.
Definition PointPerception.cpp:171
const pcl::PointCloud< pcl::PointXYZ > & as_points(int idx) const
Retrieves the filtered point cloud for a specific perception.
PointPerceptionsOpsView(const PointPerceptions &perceptions)
Constructor using a constant reference to a container of perceptions.
Definition PointPerception.cpp:148
std::shared_ptr< PointPerceptionsOpsView > fuse(const std::string &target_frame) const
Fuses all perceptions into one, transforming them to a common frame.
Definition PointPerception.cpp:287
std::shared_ptr< PointPerceptionsOpsView > collapse(const std::vector< double > &collapse_dims) const
Collapses dimensions to fixed values (e.g., project onto plane).
Definition PointPerception.cpp:234
Definition RTTFBuffer.hpp:30
sensor_msgs::msg::PointCloud2 points_to_rosmsg(const pcl::PointCloud< pcl::PointXYZ > &points)
Converts a PCL point cloud to a sensor_msgs::msg::PointCloud2 message.
Definition PointPerception.cpp:140
sensor_msgs::msg::PointCloud2 perception_to_rosmsg(const PointPerception &perception)
Converts a PointPerception to a sensor_msgs::msg::PointCloud2 message.
Definition PointPerception.cpp:130
void convert(const sensor_msgs::msg::LaserScan &scan, pcl::PointCloud< pcl::PointXYZ > &pc)
Converts a LaserScan message into a point cloud.
Definition PointPerception.cpp:98
std::vector< std::shared_ptr< PointPerception > > PointPerceptions
Alias for a vector of pointers to PointPerception objects.
Definition PointPerception.hpp:140
PointPerceptions get_point_perceptions(std::vector< PerceptionPtr > &perceptionptr)
Definition PointPerception.cpp:333
Definition PointPerception.hpp:56
Hash function for VoxelKey.
Definition PointPerception.hpp:169
std::size_t operator()(const VoxelKey &key) const
Definition PointPerception.hpp:170
Represents a discrete 3D voxel index for downsampling.
Definition PointPerception.hpp:156
int y
Definition PointPerception.hpp:157
int z
Definition PointPerception.hpp:157
int x
Definition PointPerception.hpp:157
bool operator==(const VoxelKey &other) const
Equality comparison operator.
Definition PointPerception.hpp:160
std::size_t operator()(const std::tuple< int, int, int > &key) const
Definition PointPerception.hpp:62