Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to compute point distances #29

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions include/gtsam_points/types/point_cloud_cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,30 @@ PointCloudCPU::Ptr remove_outliers(const PointCloud::ConstPtr& points, const std
*/
PointCloudCPU::Ptr remove_outliers(const PointCloud::ConstPtr& points, const int k = 10, const double std_thresh = 1.0, const int num_threads = 1);

/**
* @brief Compute point distances
* @param points Input points
* @param max_scan_count Maximum number of points to scan
* @return Distances
*/
std::vector<double> distances(const PointCloud::ConstPtr& points, size_t max_scan_count = std::numeric_limits<size_t>::max());

/**
* @brief Compute min and max point distances
* @param points Input points
* @param max_scan_count Maximum number of points to scan
* @return Min and max distances
*/
std::pair<double, double> minmax_distance(const PointCloud::ConstPtr& points, size_t max_scan_count = std::numeric_limits<size_t>::max());

/**
* @brief Compute median point distance
* @param points Input points
* @param max_scan_count Maximum number of points to scan
* @return Median distance
*/
double median_distance(const PointCloud::ConstPtr& points, size_t max_scan_count = std::numeric_limits<size_t>::max());

/**
* @brief Merge a set of frames into one frame
* @note This function only merges points and covs and discard other point attributes.
Expand Down
33 changes: 33 additions & 0 deletions src/gtsam_points/types/point_cloud_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,4 +928,37 @@ PointCloudCPU::Ptr remove_outliers(const PointCloud::ConstPtr& frame, const int
return remove_outliers(frame, neighbors, k, std_thresh);
}

std::vector<double> distances(const PointCloud::ConstPtr& points, size_t max_scan_count) {
std::vector<double> dists;
dists.reserve(points->size() < max_scan_count ? points->size() : max_scan_count * 2);

const size_t step = points->size() < max_scan_count ? 1 : points->size() / max_scan_count;
for (size_t i = 0; i < points->size(); i += step) {
const auto& p = points->points[i];
dists.emplace_back(p.head<3>().norm());
}

return dists;
}

std::pair<double, double> minmax_distance(const PointCloud::ConstPtr& points, size_t max_scan_count) {
const auto dists = distances(points, max_scan_count);
if (dists.empty()) {
return {0.0, 0.0};
}

const auto minmax = std::minmax_element(dists.begin(), dists.end());
return {*minmax.first, *minmax.second};
}

double median_distance(const PointCloud::ConstPtr& points, size_t max_scan_count) {
auto dists = distances(points, max_scan_count);
if (dists.empty()) {
return 0.0;
}

std::nth_element(dists.begin(), dists.begin() + dists.size() / 2, dists.end());
return dists[dists.size() / 2];
}

} // namespace gtsam_points
Loading