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

Nacho/get rid of get adjacent voxels #291

Merged
merged 4 commits into from
Mar 18, 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
21 changes: 20 additions & 1 deletion cpp/kiss_icp/core/Registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,29 @@ void TransformPoints(const Sophus::SE3d &T, std::vector<Eigen::Vector3d> &points
[&](const auto &point) { return T * point; });
}

using Voxel = kiss_icp::VoxelHashMap::Voxel;
std::vector<Voxel> GetAdjacentVoxels(const Voxel &voxel, int adjacent_voxels = 1) {
std::vector<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);
}
}
}
return voxel_neighborhood;
}

Eigen::Vector3d GetClosestNeighbor(const Eigen::Vector3d &point,
const kiss_icp::VoxelHashMap &voxel_map) {
const auto &query_voxels = voxel_map.GetAdjacentVoxels(point);
// Convert the point to voxel coordinates
const auto &voxel = voxel_map.PointToVoxel(point);
// Get nearby voxels on the map
const auto &query_voxels = GetAdjacentVoxels(voxel);
// Extract the points contained within the neighborhood voxels
const auto &neighbors = voxel_map.GetPoints(query_voxels);

// Find the nearest neighbor
Eigen::Vector3d closest_neighbor;
double closest_distance2 = std::numeric_limits<double>::max();
std::for_each(neighbors.cbegin(), neighbors.cend(), [&](const auto &neighbor) {
Expand Down
16 changes: 0 additions & 16 deletions cpp/kiss_icp/core/VoxelHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,6 @@

namespace kiss_icp {

std::vector<VoxelHashMap::Voxel> VoxelHashMap::GetAdjacentVoxels(const Eigen::Vector3d &point,
int adjacent_voxels) const {
auto kx = static_cast<int>(point[0] / voxel_size_);
auto ky = static_cast<int>(point[1] / voxel_size_);
auto kz = static_cast<int>(point[2] / voxel_size_);
std::vector<Voxel> voxel_neighborhood;
for (int i = kx - adjacent_voxels; i < kx + adjacent_voxels + 1; ++i) {
for (int j = ky - adjacent_voxels; j < ky + adjacent_voxels + 1; ++j) {
for (int k = kz - adjacent_voxels; k < kz + adjacent_voxels + 1; ++k) {
voxel_neighborhood.emplace_back(i, j, k);
}
}
}
return voxel_neighborhood;
}

std::vector<Eigen::Vector3d> VoxelHashMap::GetPoints(const std::vector<Voxel> &query_voxels) const {
std::vector<Eigen::Vector3d> points;
points.reserve(query_voxels.size() * static_cast<size_t>(max_points_per_voxel_));
Expand Down
7 changes: 5 additions & 2 deletions cpp/kiss_icp/core/VoxelHashMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ struct VoxelHashMap {

inline void Clear() { map_.clear(); }
inline bool Empty() const { return map_.empty(); }
inline Voxel PointToVoxel(const Eigen::Vector3d &point) const {
return Voxel(static_cast<int>(point.x() / voxel_size_),
static_cast<int>(point.y() / voxel_size_),
static_cast<int>(point.z() / voxel_size_));
}
void Update(const std::vector<Eigen::Vector3d> &points, const Eigen::Vector3d &origin);
void Update(const std::vector<Eigen::Vector3d> &points, const Sophus::SE3d &pose);
void AddPoints(const std::vector<Eigen::Vector3d> &points);
void RemovePointsFarFromLocation(const Eigen::Vector3d &origin);
std::vector<Eigen::Vector3d> Pointcloud() const;
std::vector<Eigen::Vector3d> GetPoints(const std::vector<Voxel> &query_voxels) const;
std::vector<Voxel> GetAdjacentVoxels(const Eigen::Vector3d &point,
int adjacent_voxels = 1) const;

double voxel_size_;
double max_distance_;
Expand Down
Loading