Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
skidpad_trimming.cpp
Go to the documentation of this file.
2
4
5void SkidpadTrimming::fov_trimming(const sensor_msgs::msg::PointCloud2::SharedPtr& cloud,
6 sensor_msgs::msg::PointCloud2::SharedPtr& trimmed_cloud) const {
7 // Copy metadata
8 trimmed_cloud->header = cloud->header;
9 trimmed_cloud->height = 1;
10 trimmed_cloud->is_dense = false;
11 trimmed_cloud->fields = cloud->fields;
12 trimmed_cloud->point_step = cloud->point_step;
13 trimmed_cloud->width = 0;
14 trimmed_cloud->row_step = 0;
15 trimmed_cloud->data.resize(cloud->data.size());
16
17 const auto& data = cloud->data;
18 const size_t n = cloud->width * cloud->height;
19
20 const bool rotate = params_.apply_rotation;
21
22 double cos_t = 1.0;
23 double sin_t = 0.0;
24 if (rotate) {
25 const double theta = params_.rotation * M_PI / 180.0;
26 cos_t = std::cos(theta);
27 sin_t = std::sin(theta);
28 }
29
30 for (size_t i = 0; i < n; ++i) {
31 const float x = *reinterpret_cast<const float*>(&data[LidarPoint::PointX(i)]);
32 const float y = *reinterpret_cast<const float*>(&data[LidarPoint::PointY(i)]);
33 const float z = *reinterpret_cast<const float*>(&data[LidarPoint::PointZ(i)]);
34 const float intensity = *reinterpret_cast<const float*>(&data[LidarPoint::PointIntensity(i)]);
35
36 // Skip invalid points
37 if (x == 0.0f && y == 0.0f && z == 0.0f) {
38 continue;
39 }
40
41 float rx = x;
42 float ry = y;
43
44 if (rotate) {
45 rx = static_cast<float>(x * cos_t - y * sin_t);
46 ry = static_cast<float>(x * sin_t + y * cos_t);
47 }
48
49 if (within_limits(rx, ry, z, intensity, params_, params_.skid_max_range)) {
50 uint8_t* out = &trimmed_cloud->data[trimmed_cloud->width * LidarPoint::POINT_STEP];
51
52 static_cast<void>(std::memcpy(out, &data[LidarPoint::PointX(i)], LidarPoint::POINT_STEP));
53
54 // Overwrite rotated X/Y if needed
55 if (rotate) {
56 *reinterpret_cast<float*>(out + LidarPoint::PointX(0)) = rx;
57 *reinterpret_cast<float*>(out + LidarPoint::PointY(0)) = ry;
58 }
59
60 trimmed_cloud->width++;
61 }
62 }
63
64 trimmed_cloud->data.resize(trimmed_cloud->width * LidarPoint::POINT_STEP);
65 trimmed_cloud->row_step = trimmed_cloud->width * LidarPoint::POINT_STEP;
66}
bool within_limits(float x, float y, float z, float intensity, const TrimmingParameters &params, const double max_range) const
TrimmingParameters params_
SkidpadTrimming()=default
Default constructor.
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.
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 skid_max_range
(Skidpad Only) Maximum 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.