Formula Student Autonomous Systems
The code for the main driverless system
Loading...
Searching...
No Matches
cluster.cpp
Go to the documentation of this file.
1#include <utils/cluster.hpp>
2
3Cluster::Cluster(const sensor_msgs::msg::PointCloud2::SharedPtr& point_cloud,
4 const std::vector<int>& point_indices)
5 : _point_cloud_(point_cloud), _point_indices_(point_indices) {}
6
7Eigen::Vector4f Cluster::get_centroid() {
9 return _centroid_;
10 }
11
12 this->_centroid_ =
13 Cluster::centroid_calculator.calculate_center(this->_point_cloud_, this->_point_indices_);
14 this->_centroid_is_defined_ = true;
15 return this->_centroid_;
16}
17
18Eigen::Vector4f Cluster::get_center(Plane& plane) {
20 return this->_center_;
21 }
22
23 this->_center_ = Cluster::center_calculator.calculate_center(this->_point_cloud_,
24 this->_point_indices_, plane);
25 this->_center_is_defined_ = true;
26
27 return this->_center_;
28}
29
30std::string Cluster::get_color() { return _color_; }
31
32void Cluster::set_color(const std::string& new_color) {
33 if (new_color == "blue" || new_color == "yellow") {
34 this->_color_ = new_color;
35 }
36}
37
38void Cluster::set_point_indices(const std::vector<int>& new_point_indices) {
39 this->_point_indices_ = new_point_indices;
40 this->_center_is_defined_ = false;
41 this->_centroid_is_defined_ = false;
42}
43
44const std::vector<int>& Cluster::get_point_indices() { return this->_point_indices_; }
45
46const sensor_msgs::msg::PointCloud2::SharedPtr& Cluster::get_point_cloud() {
47 return this->_point_cloud_;
48}
49
50void Cluster::set_confidence(double new_confidence) { this->_confidence_ = new_confidence; }
51
52double Cluster::get_confidence() { return this->_confidence_; }
53
54void Cluster::set_z_score(double mean_x, double std_dev_x, double mean_y, double std_dev_y) {
55 _z_score_x_ = std::abs(this->get_centroid().x() - mean_x) / std_dev_x;
56 _z_score_y_ = std::abs(this->get_centroid().y() - mean_y) / std_dev_y;
57 if (std_dev_x == 0) {
58 _z_score_x_ = 1;
59 }
60 if (std_dev_y == 0) {
61 _z_score_y_ = 1;
62 }
63}
64
65double Cluster::get_z_score_x() const { return _z_score_x_; }
66
67double Cluster::get_z_score_y() const { return _z_score_y_; }
68
69std::tuple<double, double, double, double> Cluster::calculate_mean_and_std_dev(
70 std::vector<Cluster>& clusters) {
71 double sum_x = 0.0;
72 double sum_y = 0.0;
73 for (auto& point : clusters) {
74 sum_x += point.get_centroid().x();
75 sum_y += point.get_centroid().y();
76 }
77
78 double mean_x = sum_x / static_cast<double>(clusters.size());
79 double mean_y = sum_y / static_cast<double>(clusters.size());
80
81 double sum_squared_diff_x = 0.0;
82 double sum_squared_diff_y = 0.0;
83 for (auto& point : clusters) {
84 sum_squared_diff_x += std::pow(point.get_centroid().x() - mean_x, 2);
85 sum_squared_diff_y += std::pow(point.get_centroid().y() - mean_y, 2);
86 }
87
88 double stddev_x = std::sqrt(sum_squared_diff_x / static_cast<double>(clusters.size()));
89 double stddev_y = std::sqrt(sum_squared_diff_y / static_cast<double>(clusters.size()));
90
91 return std::make_tuple(mean_x, mean_y, stddev_x, stddev_y);
92}
93
94void Cluster::set_z_scores(std::vector<Cluster>& clusters) {
95 auto [mean_x, mean_y, stddev_x, stddev_y] = calculate_mean_and_std_dev(clusters);
96 for (auto& cluster : clusters) {
97 cluster.set_z_score(mean_x, stddev_x, mean_y, stddev_y);
98 }
99}
100
102
void set_is_large()
Set cluster as a contender for a large cone.
Definition cluster.cpp:103
const std::vector< int > & get_point_indices()
Get the Point Cloud data of the cluster.
Definition cluster.cpp:44
void set_confidence(double newConfidence)
Set the Confidence of the cluster to be or not to be a cone.
Definition cluster.cpp:50
double get_z_score_y() const
Get the z score on y-axis of an object.
Definition cluster.cpp:67
void set_z_score(double mean_x, double std_dev_x, double mean_y, double std_dev_y)
Set the z score object on the x and y axis.
Definition cluster.cpp:54
std::string _color_
Color associated with the cluster.
Definition cluster.hpp:135
double _confidence_
Confidence on the cluster to be (or not) a cone.
Definition cluster.hpp:140
const sensor_msgs::msg::PointCloud2::SharedPtr & get_point_cloud()
Get the Point Cloud data of the cluster.
Definition cluster.cpp:46
double get_z_score_x() const
Get the z score on x-axis of an object.
Definition cluster.cpp:65
bool _center_is_defined_
Flag indicating whether the center is defined or not.
Definition cluster.hpp:139
double _z_score_y_
Definition cluster.hpp:142
std::vector< int > _point_indices_
Indices of points in the cluster.
Definition cluster.hpp:134
Eigen::Vector4f _center_
Center of the cone's cluster.
Definition cluster.hpp:137
double get_confidence()
Get the Confidence of the cluster to be (or not to be) a cone.
Definition cluster.cpp:52
static void set_z_scores(std::vector< Cluster > &clusters)
Set the z scores object on every cluster of the vector.
Definition cluster.cpp:94
Eigen::Vector4f _centroid_
Centroid of the cluster.
Definition cluster.hpp:136
std::string get_color()
Get the color associated with the cluster.
Definition cluster.cpp:30
Eigen::Vector4f get_center(Plane &plane)
Get the Center of the cone's cluster.
Definition cluster.cpp:18
Eigen::Vector4f get_centroid()
Get the centroid of the cluster.
Definition cluster.cpp:7
Cluster(const sensor_msgs::msg::PointCloud2::SharedPtr &point_cloud, const std::vector< int > &point_indices)
Constructor for the Cluster class.
Definition cluster.cpp:3
double _z_score_x_
Definition cluster.hpp:141
bool get_is_large()
Get cluster's corresponding cone size.
Definition cluster.cpp:101
const sensor_msgs::msg::PointCloud2::SharedPtr _point_cloud_
Point cloud data for the cluster.
Definition cluster.hpp:133
void set_color(const std::string &new_color)
Set the color for the cluster.
Definition cluster.cpp:32
bool _is_large_
Flag indicating the size of the cluster :
Definition cluster.hpp:143
static constexpr auto center_calculator
Calculates the center of the cone.
Definition cluster.hpp:147
bool _centroid_is_defined_
Flag indicating whether the centroid is defined or not.
Definition cluster.hpp:138
static std::tuple< double, double, double, double > calculate_mean_and_std_dev(std::vector< Cluster > &clusters)
Calculates the mean and standard deviation on x and y axis.
Definition cluster.cpp:69
void set_point_indices(const std::vector< int > &new_point_indices)
Set the Point Cloud data for the cluster.
Definition cluster.cpp:38
static constexpr auto centroid_calculator
Calculates the centroid of the cluster.
Definition cluster.hpp:149
The Plane class represents a 3D plane defined by its equation ax + by + cz + d = 0.
Definition plane.hpp:12