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

Simplify GetClosestPoint #391

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 19 additions & 20 deletions cpp/kiss_icp/core/VoxelHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@
#include "VoxelUtils.hpp"

namespace {
using kiss_icp::Voxel;

std::vector<Voxel> GetAdjacentVoxels(const Voxel &voxel, int adjacent_voxels = 1) {
std::vector<Voxel> voxel_neighborhood;
std::vector<kiss_icp::Voxel> GetAdjacentVoxels(const Eigen::Vector3d &point,
const kiss_icp::VoxelHashMap &grid,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why pass this as an argument? How should I read this line?

    const auto &query_voxels = GetAdjacentVoxels(query, *this);

const int adjacent_voxels = 1) {
// Convert the point to voxel coordinates
const auto &voxel = kiss_icp::PointToVoxel(point, grid.voxel_size_);
std::vector<kiss_icp::Voxel> voxel_neighborhood;
for (int i = voxel.x() - adjacent_voxels; i < voxel.x() + adjacent_voxels + 1; ++i) {
for (int j = voxel.y() - adjacent_voxels; j < voxel.y() + adjacent_voxels + 1; ++j) {
for (int k = voxel.z() - adjacent_voxels; k < voxel.z() + adjacent_voxels + 1; ++k) {
voxel_neighborhood.emplace_back(i, j, k);
if (grid.map_.contains({i, j, k})) {
voxel_neighborhood.emplace_back(i, j, k);
}
}
}
}
Expand All @@ -49,26 +53,21 @@ namespace kiss_icp {

std::tuple<Eigen::Vector3d, double> VoxelHashMap::GetClosestNeighbor(
const Eigen::Vector3d &query) const {
// Convert the point to voxel coordinates
const auto &voxel = PointToVoxel(query, voxel_size_);
// Get nearby voxels on the map
const auto &query_voxels = GetAdjacentVoxels(voxel);
const auto &query_voxels = GetAdjacentVoxels(query, *this);
// Find the nearest neighbor
Eigen::Vector3d closest_neighbor = Eigen::Vector3d::Zero();
double closest_distance = std::numeric_limits<double>::max();
std::for_each(query_voxels.cbegin(), query_voxels.cend(), [&](const auto &query_voxel) {
auto search = map_.find(query_voxel);
if (search != map_.end()) {
const auto &points = search.value();
const Eigen::Vector3d &neighbor = *std::min_element(
points.cbegin(), points.cend(), [&](const auto &lhs, const auto &rhs) {
return (lhs - query).norm() < (rhs - query).norm();
});
double distance = (neighbor - query).norm();
if (distance < closest_distance) {
closest_neighbor = neighbor;
closest_distance = distance;
}
const auto &points = map_.at(query_voxel);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is relatively unsafe. I know that up here, you only provide the voxels that already exist in the grid, but within this method, nothing prevents us from making a mistake; in that case, we will get an exception

TSL_RH_THROW_OR_TERMINATE(std::out_of_range, "Couldn't find key.");

const Eigen::Vector3d &neighbor = *std::min_element(
points.cbegin(), points.cend(), [&](const auto &lhs, const auto &rhs) {
return (lhs - query).norm() < (rhs - query).norm();
});
double distance = (neighbor - query).norm();
if (distance < closest_distance) {
closest_neighbor = neighbor;
closest_distance = distance;
}
});
return std::make_tuple(closest_neighbor, closest_distance);
Expand Down
Loading