Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
grid_clustering.cpp
Go to the documentation of this file.
2
3GridClustering::GridClustering(double grid_angle, double grid_radius, double start_augmentation,
4 double radius_augmentation, double fov)
5 : grid_geometry_(grid_angle, grid_radius, start_augmentation, radius_augmentation, fov) {}
6
7void GridClustering::clustering(const sensor_msgs::msg::PointCloud2::SharedPtr& input_cloud,
8 std::vector<Cluster>* clusters) const {
9 const auto& cloud_data = input_cloud->data;
10 const size_t num_points = input_cloud->width * input_cloud->height;
11 if (num_points == 0) {
12 return;
13 }
14
15 std::unordered_map<GridIndex, std::vector<size_t>> grid_map;
16 for (size_t i = 0; i < num_points; ++i) {
17 float x = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointX(i)]);
18 float y = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointY(i)]);
19 if (x == 0.0f && y == 0.0f) {
20 continue;
21 }
22
23 int gx = grid_geometry_.get_slice_index(x, y);
24 int gy = grid_geometry_.get_bin_index(x, y);
25 if (gx == -1) {
26 continue; // Out of FOV
27 }
28 grid_map[{gx, gy}].push_back(i);
29 }
30
31 std::unordered_map<GridIndex, bool> visited;
32 for (const auto& [cell, points] : grid_map) {
33 if (visited[cell]) {
34 continue;
35 }
36 visited[cell] = true;
37
38 std::queue<GridIndex> q;
39 q.push(cell);
40 std::vector<int> cluster_points;
41
42 while (!q.empty()) {
43 GridIndex current = q.front();
44 q.pop();
45
46 auto it = grid_map.find(current);
47 if (it == grid_map.end()) {
48 continue;
49 }
50
51 for (size_t idx : it->second) {
52 cluster_points.push_back(static_cast<int>(idx));
53 }
54
55 for (int dx = -1; dx <= 1; ++dx) {
56 for (int dy = -1; dy <= 1; ++dy) {
57 if (dx == 0 && dy == 0) {
58 continue;
59 }
60 GridIndex neighbor{current.x + dx, current.y + dy};
61 if (visited[neighbor]) {
62 continue;
63 }
64 if (grid_map.find(neighbor) != grid_map.end()) {
65 visited[neighbor] = true;
66 q.push(neighbor);
67 }
68 }
69 }
70 }
71 clusters->push_back(Cluster(input_cloud, cluster_points));
72 }
73}
Represents a cluster of 3D points using PCL (Point Cloud Library).
Definition cluster.hpp:14
void clustering(const sensor_msgs::msg::PointCloud2::SharedPtr &point_cloud, std::vector< Cluster > *clusters) const override
Clusters the input point cloud into groups using the GridClustering algorithm.
GridGeometry grid_geometry_
Geometry of the grid used for clustering.
GridClustering(double grid_angle, double grid_radius, double start_augmentation, double radius_augmentation, double fov)
Constructor for the GridClustering algorithm.
constexpr size_t PointX(size_t idx)
constexpr size_t PointY(size_t idx)
int get_bin_index(double x, double y) const
Compute bin index for a given (x, y)
int get_slice_index(double x, double y) const
Compute slice index for a given (x, y)
Structure to represent a grid index with x and y coordinates, needed for grid mapping.