Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
cylinder_validator.cpp
Go to the documentation of this file.
2
3CylinderValidator::CylinderValidator(double small_width, double small_height, double large_width,
4 double large_height, double out_distance_cap)
5 : small_width(small_width),
6 small_height(small_height),
7 large_width(large_width),
8 large_height(large_height),
9 out_distance_cap(out_distance_cap) {}
10
11double CylinderValidator::small_getRadius() const { return small_width / 2; }
12
13double CylinderValidator::large_getRadius() const { return large_width / 2; }
14
15std::vector<double> CylinderValidator::coneValidator(Cluster* cone_cluster,
16 [[maybe_unused]] Plane& plane) const {
17 const auto& cloud_data = cone_cluster->get_point_cloud()->data;
18 const auto& indices = cone_cluster->get_point_indices();
19
20 double out_distanceXY = 1.0;
21 double out_distanceZ = 1.0;
22 int n_out_points = 0;
23
24 // Loop over points in the cluster
25 for (size_t idx : indices) {
26 float x = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointX(idx)]);
27 float y = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointY(idx)]);
28 float z = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointZ(idx)]);
29
30 // Calculate the distance between the point and the cylinder's centroid
31 double distanceXY =
32 std::sqrt((x - cone_cluster->get_centroid().x()) * (x - cone_cluster->get_centroid().x()) +
33 (y - cone_cluster->get_centroid().y()) * (y - cone_cluster->get_centroid().y()));
34
35 // Calculate distance between the point and the centroid along the z-axis
36 double distanceZ = std::abs(z - cone_cluster->get_centroid().z());
37
38 // Choose which cylinder to use depending on the size of the cluster
39 if (cone_cluster->get_is_large() &&
40 (distanceXY > large_getRadius() || distanceZ > large_height / 2)) {
41 n_out_points++;
42 out_distanceXY = std::min(out_distanceXY, large_getRadius() / distanceXY);
43 out_distanceZ = std::min(out_distanceZ, large_height / (2 * distanceZ));
44
45 } else if (distanceXY > small_getRadius() || distanceZ > small_height / 2) {
46 n_out_points++;
47 out_distanceXY = std::min(out_distanceXY, small_getRadius() / distanceXY);
48 out_distanceZ = std::min(out_distanceZ, small_height / (2 * distanceZ));
49 } else {
50 // Point is inside the cylinder
51 out_distanceXY = 1.0;
52 out_distanceZ = 1.0;
53 }
54
55 // Apply cap thresholds
56 if (out_distanceXY < out_distance_cap) {
57 out_distanceXY = 0.0;
58 }
59 if (out_distanceZ < out_distance_cap) {
60 out_distanceZ = 0.0;
61 }
62 }
63
64 // index 0 = ratio of distance to the farthest point / cylinder radius
65 // index 1 = ratio of distance to the farthest point / cylinder height
66 // index 2 = ratio of points outside the cylinder
67 return {out_distanceXY, out_distanceZ, 1.0 - static_cast<double>(n_out_points) / indices.size()};
68}
Represents a cluster of 3D points using PCL (Point Cloud Library).
Definition cluster.hpp:14
const std::vector< int > & get_point_indices()
Get the Point Cloud data of the cluster.
Definition cluster.cpp:44
const sensor_msgs::msg::PointCloud2::SharedPtr & get_point_cloud()
Get the Point Cloud data of the cluster.
Definition cluster.cpp:46
Eigen::Vector4f get_centroid()
Get the centroid of the cluster.
Definition cluster.cpp:7
bool get_is_large()
Get cluster's corresponding cone size.
Definition cluster.cpp:101
std::vector< double > coneValidator(Cluster *cone_point_cloud, Plane &plane) const override
Validates a cluster using cylinder approximation.
double large_height
Height of the cylinder for a large cone.
double small_getRadius() const
Gets the radius of the cylinder for small cones.
double small_width
Width of the cylinder for a small cone.
double large_width
Width of the cylinder for a large cone.
CylinderValidator(double small_width, double small_height, double large_width, double large_height, double out_distance_cap)
Constructs a new CylinderValidator object with specified width and height.
double large_getRadius() const
Gets the radius of the cylinder for large cones.
double out_distance_cap
Minimum out_distance value for it to be 0.
double small_height
Height of the cylinder for a small cone.
The Plane class represents a 3D plane defined by its equation ax + by + cz + d = 0.
Definition plane.hpp:12
constexpr size_t PointZ(size_t idx)
constexpr size_t PointX(size_t idx)
constexpr size_t PointY(size_t idx)