Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
cut_trimming.cpp
Go to the documentation of this file.
2
3#include <cstring>
4
6
7void CutTrimming::fov_trimming(const sensor_msgs::msg::PointCloud2::SharedPtr& cloud,
8 sensor_msgs::msg::PointCloud2::SharedPtr& trimmed_cloud) const {
9 // Copy metadata
10 trimmed_cloud->header = cloud->header;
11 trimmed_cloud->height = 1;
12 trimmed_cloud->is_dense = false;
13 trimmed_cloud->fields = cloud->fields;
14 trimmed_cloud->point_step = cloud->point_step;
15 trimmed_cloud->width = 0;
16 trimmed_cloud->row_step = 0;
17 trimmed_cloud->data.resize(cloud->data.size());
18
19 const auto& data = cloud->data;
20 const size_t n = cloud->width * cloud->height;
21
22 const bool rotate = params_.apply_rotation;
23
24 double cos_t = 1.0;
25 double sin_t = 0.0;
26 if (rotate) {
27 const double theta = params_.rotation * M_PI / 180.0;
28 cos_t = std::cos(theta);
29 sin_t = std::sin(theta);
30 }
31
32 for (size_t i = 0; i < n; ++i) {
33 const float x = *reinterpret_cast<const float*>(&data[LidarPoint::PointX(i)]);
34 const float y = *reinterpret_cast<const float*>(&data[LidarPoint::PointY(i)]);
35 const float z = *reinterpret_cast<const float*>(&data[LidarPoint::PointZ(i)]);
36 const float intensity = *reinterpret_cast<const float*>(&data[LidarPoint::PointIntensity(i)]);
37
38 // Skip invalid points
39 if (x == 0.0f && y == 0.0f && z == 0.0f) {
40 continue;
41 }
42
43 float rx = x;
44 float ry = y;
45
46 if (rotate) {
47 rx = static_cast<float>(x * cos_t - y * sin_t);
48 ry = static_cast<float>(x * sin_t + y * cos_t);
49 }
50
51 if (within_limits(rx, ry, z, intensity, params_, params_.max_range)) {
52 uint8_t* out = &trimmed_cloud->data[trimmed_cloud->width * LidarPoint::POINT_STEP];
53
54 static_cast<void>(std::memcpy(out, &data[LidarPoint::PointX(i)], LidarPoint::POINT_STEP));
55
56 // Overwrite X/Y only if rotation is active
57 if (rotate) {
58 *reinterpret_cast<float*>(out + LidarPoint::PointX(0)) = rx;
59 *reinterpret_cast<float*>(out + LidarPoint::PointY(0)) = ry;
60 }
61
62 trimmed_cloud->width++;
63 }
64 }
65
66 trimmed_cloud->data.resize(trimmed_cloud->width * LidarPoint::POINT_STEP);
67 trimmed_cloud->row_step = trimmed_cloud->width * LidarPoint::POINT_STEP;
68}
void fov_trimming(const sensor_msgs::msg::PointCloud2::SharedPtr &point_cloud, sensor_msgs::msg::PointCloud2::SharedPtr &trimmed_cloud) const override
Perform ground removal on the input point cloud.
CutTrimming()=default
Default constructor.
bool within_limits(float x, float y, float z, float intensity, const TrimmingParameters &params, const double max_range) const
TrimmingParameters params_
constexpr size_t PointZ(size_t idx)
constexpr size_t POINT_STEP
constexpr size_t PointX(size_t idx)
constexpr size_t PointIntensity(size_t idx)
constexpr size_t PointY(size_t idx)
Structure to hold parameters for trimming point cloud data.
double max_range
Minimum point cloud distance after trimming.
double rotation
Rotation angle to be applied to the point cloud.
bool apply_rotation
Whether to apply rotation to the point cloud.