Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
cylinder_validator_test.cpp
Go to the documentation of this file.
1#include <gtest/gtest.h>
2#include <pcl/point_cloud.h>
3#include <pcl/point_types.h>
4
6#include <utils/cluster.hpp>
7#include <utils/plane.hpp>
8
9// Non-owning deleter: does nothing.
10template <typename T>
12 void operator()(T*) const {}
13};
14
18class CylinderValidatorTest : public ::testing::Test {
19protected:
23 void SetUp() override { plane = Plane(1, 0, 0, 0); }
25};
26
30TEST_F(CylinderValidatorTest, PointsInsideSmallCylinder) {
31 const CylinderValidator validator(0.5, 1.0, 0.7, 1.5, 0.5);
32
33 pcl::PointCloud<pcl::PointXYZI> cloud;
34 cloud.points.push_back(pcl::PointXYZI{0.3, 0.3, 0.5, 0});
35 const pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_ptr(
36 &cloud, NonOwningDeleter<pcl::PointCloud<pcl::PointXYZI>>());
37
38 Cluster cylinderPointCloud(cloud_ptr);
39
40 std::vector<double> result = validator.coneValidator(&cylinderPointCloud, plane);
41
42 // The centroid is at (0.3, 0.3, 0.5), so the distance to the point is 0.
43
44 ASSERT_NEAR(result[0], 1.0, 1e-6); // Distance XY ratio is 1 because the point is the centroid.
45 ASSERT_NEAR(result[1], 1.0, 1e-6); // Distance Z ratio is 1 because the point is the centroid.
46 ASSERT_DOUBLE_EQ(result[2], 1.0); // All points are inside the cylinder.
47}
48
52TEST_F(CylinderValidatorTest, PointsInsideLargeCylinder) {
53 const CylinderValidator validator(0.5, 1.0, 0.7, 1.5, 0.5);
54
55 pcl::PointCloud<pcl::PointXYZI> cloud;
56 cloud.points.push_back(pcl::PointXYZI{0.6, 0.6, 1.2, 0});
57 cloud.points.push_back(pcl::PointXYZI{0.3, 0.3, 0.5, 0});
58 const pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_ptr(
59 &cloud, NonOwningDeleter<pcl::PointCloud<pcl::PointXYZI>>());
60
61 Cluster cylinderPointCloud(cloud_ptr);
62 // Simulate height validator and set cluster as large.
63 cylinderPointCloud.set_is_large();
64
65 std::vector<double> result = validator.coneValidator(&cylinderPointCloud, plane);
66
67 // The centroid is at (0.45, 0.45, 0.85)
68 // Both points are inside the large cylinder so the ratios are 1.
69
70 ASSERT_NEAR(result[0], 1.0, 1e-6); // Distance XY ratio is 1.
71 ASSERT_NEAR(result[1], 1.0, 1e-6); // Distance Z ratio is 1.
72 ASSERT_DOUBLE_EQ(result[2], 1.0); // All points are inside the cylinder.
73}
74
78TEST_F(CylinderValidatorTest, PointsFarOutsideCylinders) {
79 const CylinderValidator validator(0.5, 1.0, 0.7, 1.5, 0.5);
80
81 pcl::PointCloud<pcl::PointXYZI> cloud;
82 cloud.points.push_back(pcl::PointXYZI{1.0, 1.0, 2.0, 0});
83 cloud.points.push_back(pcl::PointXYZI{20.0, 20.0, 20.0, 0});
84 cloud.points.push_back(pcl::PointXYZI{0.3, 0.3, 0.5, 0});
85 const pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_ptr(
86 &cloud, NonOwningDeleter<pcl::PointCloud<pcl::PointXYZI>>());
87
88 Cluster cylinderPointCloud(cloud_ptr);
89
90 std::vector<double> result = validator.coneValidator(&cylinderPointCloud, plane);
91
92 // The centroid is at (7.1, 7.1, 7.5)
93 // All points are outside the cylinders, so the ratios are capped to 0.
94
95 ASSERT_NEAR(result[0], 0.0, 1e-6); // Distance XY ratio is 0.
96 ASSERT_NEAR(result[1], 0.0, 1e-6); // Distance Z ratio is 0.
97 ASSERT_LT(result[2], 1.0); // Some points are outside the cylinder.
98}
99
103TEST_F(CylinderValidatorTest, PointsOutsideCylinders) {
104 const CylinderValidator validator(0.5, 1.0, 0.7, 1.5, 0.5);
105
106 pcl::PointCloud<pcl::PointXYZI> cloud;
107 cloud.points.push_back(pcl::PointXYZI{1.0, 1.0, 2.0, 0});
108 cloud.points.push_back(pcl::PointXYZI{0.6, 0.6, 0.9, 0});
109 cloud.points.push_back(pcl::PointXYZI{0.3, 0.3, 0.5, 0});
110 const pcl::PointCloud<pcl::PointXYZI>::Ptr cloud_ptr(
111 &cloud, NonOwningDeleter<pcl::PointCloud<pcl::PointXYZI>>());
112
113 Cluster cylinderPointCloud(cloud_ptr);
114
115 std::vector<double> result = validator.coneValidator(&cylinderPointCloud, plane);
116
117 // Centroid is at (0.633333, 0.633333, 1.13333)
118 // Distance to (1,1,2) is ~1.04, so ratio is 0.5/1.04 ~ 0.48, so it is capped to 0.0
119
120 ASSERT_EQ(result[0], 0.0); // Distance XY ratio is capped to 0.0.
121 ASSERT_GE(result[1], 0.5); // Distance Z ratio is higher than cap.
122 ASSERT_LE(result[1], 1.0); // Distance Z ratio is capped at 1.
123 ASSERT_LT(result[2], 1.0); // Some points are outside the cylinder.
124}
Represents a cluster of 3D points using PCL (Point Cloud Library).
Definition cluster.hpp:14
void set_is_large()
Set cluster as a contender for a large cone.
Definition cluster.cpp:103
Validates clusters using a cylinder approximation.
std::vector< double > coneValidator(Cluster *cone_point_cloud, Plane &plane) const override
Validates a cluster using cylinder approximation.
Test fixture for CylinderValidator class.
void SetUp() override
Set up function to initialize.
Plane plane
Plane object for testing.
The Plane class represents a 3D plane defined by its equation ax + by + cz + d = 0.
Definition plane.hpp:12
TEST_F(CylinderValidatorTest, PointsInsideSmallCylinder)
Test case to validate if points are inside the small cylinder.