Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
deviation_validator.cpp
Go to the documentation of this file.
2
3DeviationValidator::DeviationValidator(double min_xoy, double max_xoy, double min_z, double max_z)
4 : _min_xoy_(min_xoy), _max_xoy_(max_xoy), _min_z_(min_z), _max_z_(max_z) {}
5
6std::vector<double> DeviationValidator::coneValidator(Cluster* cone_cluster,
7 [[maybe_unused]] Plane& plane) const {
8 // Vectors to store deviations in XOY and Z
9 std::vector<double> deviations_xoy;
10 std::vector<double> deviations_z;
11
12 const auto& cloud_data = cone_cluster->get_point_cloud()->data;
13 const auto& indices = cone_cluster->get_point_indices();
14
15 // Calculate the mean point in XOY and Z
16 double mean_x = 0.0;
17 double mean_y = 0.0;
18 double mean_z = 0.0;
19 for (size_t idx : indices) {
20 float x = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointX(idx)]);
21 float y = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointY(idx)]);
22 float z = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointZ(idx)]);
23
24 mean_x += x;
25 mean_y += y;
26 mean_z += z;
27 }
28 mean_x /= static_cast<double>(indices.size());
29 mean_y /= static_cast<double>(indices.size());
30 mean_z /= static_cast<double>(indices.size());
31
32 // Calculate deviations from the mean
33 for (size_t idx : indices) {
34 float x = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointX(idx)]);
35 float y = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointY(idx)]);
36 float z = *reinterpret_cast<const float*>(&cloud_data[LidarPoint::PointZ(idx)]);
37
38 double deviation_xoy = std::sqrt(std::pow(x - mean_x, 2) + std::pow(y - mean_y, 2));
39 deviations_xoy.push_back(deviation_xoy);
40 deviations_z.push_back(std::abs(z - mean_z));
41 }
42
43 // Calculate the standard deviation of the deviations
44 auto calc_std_dev = [](const std::vector<double>& deviations) {
45 double sum = std::accumulate(deviations.begin(), deviations.end(), 0.0);
46 double mean = sum / static_cast<double>(deviations.size());
47 double sq_sum =
48 std::inner_product(deviations.begin(), deviations.end(), deviations.begin(), 0.0);
49 double variance = sq_sum / static_cast<double>(deviations.size()) - mean * mean;
50 return std::sqrt(variance);
51 };
52
53 double std_dev_xoy = calc_std_dev(deviations_xoy);
54 double std_dev_z = calc_std_dev(deviations_z);
55
56 double xoy = 0;
57 if (_min_xoy_ > 0) {
58 xoy = std::min({std_dev_xoy / _min_xoy_, _max_xoy_ / std_dev_xoy, 1.0});
59 } else {
60 xoy = std::min({_max_xoy_ / std_dev_xoy, 1.0});
61 }
62
63 double z = 0;
64 if (_min_z_ > 0) {
65 z = std::min({std_dev_z / _min_z_, _max_z_ / std_dev_z, 1.0});
66 } else {
67 z = std::min({_max_z_ / std_dev_z, 1.0});
68 }
69
70 // index 0 = if not in xoy interval, ratio between the std deviation of the cluster and the
71 // maximum or minimum deviation, whichever is closest to.
72 // index 1 = if not z in interval, ratio between the std deviation of the cluster and the maximum
73 // or minimum deviation, whichever is the closest to z
74 return {xoy, z};
75}
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
double _max_z_
Maximum z axis deviation.
DeviationValidator(double min_xoy, double max_xoy, double min_z, double max_z)
Constructs a new DeviationValidator object with specified intervals on the deviation.
std::vector< double > coneValidator(Cluster *cone_point_cloud, Plane &plane) const override
Validates a cluster using standard deviation.
double _min_xoy_
Minimum xOy plane deviation.
double _min_z_
Minimum z axis deviation.
double _max_xoy_
Maximum xOy plane deviation.
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)