I have a point cloud of some data of 24 xyz points. Im trying to calculate the distance between each point and checking to see if the distance is less than 10, then save the points in closestPoints. But my code gets stuck at the last iteration and doesnt save closestPoints.pcd
// Euclidean distance
for (size_t i = 0; i < allCornerPoints->size()-1; ++i) {
const pcl::PointXYZ& point1 = allCornerPoints->points[i];
std::cout << "Distances from point (" << point1.x << ", " << point1.y << ", " << point1.z << "):" << std::endl;
std::cout << i << std::endl;
if (!(i == allCornerPoints->size() - 1)) {
for (size_t j = i + 1; j < allCornerPoints->size(); ++j) {
const pcl::PointXYZ& point2 = allCornerPoints->points[j];
std::cout << j << std::endl;
// Calculate the Euclidean distance
float distanceSquared = std::pow(point1.x - point2.x, 2) +
std::pow(point1.y - point2.y, 2) +
std::pow(point1.z - point2.z, 2);
float distance = std::sqrt(distanceSquared);
std::cout << " To point (" << point2.x << ", " << point2.y << ", " << point2.z << "): " << distance << std::endl;
if (distance < 10) {
// Add points to closestPoints if distance is less than 10mm
closestPoints->points.push_back(point1);
closestPoints->points.push_back(point2);
}
}
}
}
pcl::io::savePCDFileASCII("closestPoints.pcd", *closestPoints);
std::cout << " closest points saved" << std::endl;
im exepcting that a new pcd file gets saved with closest points