4 const sensor_msgs::msg::PointCloud2::SharedPtr& point_cloud,
5 const std::vector<int>& point_indices,
const Plane& plane)
const {
6 Eigen::Vector4f centroid;
13 const auto& cloud_data = point_cloud->data;
15 for (
int idx : point_indices) {
25 size_t n = point_indices.size();
26 centroid << sum_x / n, sum_y / n, sum_z / n, 1.0f;
29 double d = -(plane.
get_a() * centroid.x() + plane.
get_b() * centroid.y() +
30 plane.
get_c() * centroid.z());
35 std::vector<Eigen::Vector3f> closest_points(3, Eigen::Vector3f::Zero());
36 std::vector<double> distances(3, std::numeric_limits<double>::max());
38 for (
int idx : point_indices) {
43 Eigen::Vector3f point(x, y, z);
48 for (
int i = 0; i < 3; ++i) {
49 if (distance < distances[i]) {
50 for (
int j = 2; j > i; --j) {
51 closest_points[j] = closest_points[j - 1];
52 distances[j] = distances[j - 1];
54 closest_points[i] = point;
55 distances[i] = distance;
63 Eigen::Vector4f center;
65 double x1 = closest_points[0].x();
66 double y1 = closest_points[0].y();
67 double x2 = closest_points[1].x();
68 double y2 = closest_points[1].y();
69 double x3 = closest_points[2].x();
70 double y3 = closest_points[2].y();
72 double D = 2 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
73 center.x() = ((x1 * x1 + y1 * y1) * (y2 - y3) + (x2 * x2 + y2 * y2) * (y3 - y1) +
74 (x3 * x3 + y3 * y3) * (y1 - y2)) /
76 center.y() = ((x1 * x1 + y1 * y1) * (x3 - x2) + (x2 * x2 + y2 * y2) * (x1 - x3) +
77 (x3 * x3 + y3 * y3) * (x2 - x1)) /
79 center.z() = centroid.z();
Eigen::Vector4f calculate_center(const sensor_msgs::msg::PointCloud2::SharedPtr &point_cloud, const std::vector< int > &point_indices, const Plane &plane=Plane()) const override
Estimates the center position of a cone using a circumference-based approach.