-
Notifications
You must be signed in to change notification settings - Fork 329
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?