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

Revise Voxel functionalities in VoxelHashMap and Preprocessing #362

Merged
merged 13 commits into from
Jul 20, 2024
Merged
34 changes: 9 additions & 25 deletions cpp/kiss_icp/core/Preprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,26 @@
#include <Eigen/Core>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <sophus/se3.hpp>
#include <vector>

namespace {
// TODO(all): Maybe try to merge these voxel uitls with VoxelHashMap implementation
using Voxel = Eigen::Vector3i;
struct VoxelHash {
size_t operator()(const Voxel &voxel) const {
const uint32_t *vec = reinterpret_cast<const uint32_t *>(voxel.data());
return ((1 << 20) - 1) & (vec[0] * 73856093 ^ vec[1] * 19349669 ^ vec[2] * 83492791);
}
};

Voxel PointToVoxel(const Eigen::Vector3d &point, double voxel_size) {
return Voxel(static_cast<int>(std::floor(point.x() / voxel_size)),
static_cast<int>(std::floor(point.y() / voxel_size)),
static_cast<int>(std::floor(point.z() / voxel_size)));
}
} // namespace
#include "VoxelUtils.hpp"

namespace kiss_icp {
std::vector<Eigen::Vector3d> VoxelDownsample(const std::vector<Eigen::Vector3d> &frame,
double voxel_size) {
tsl::robin_map<Voxel, Eigen::Vector3d, VoxelHash> grid;
tsl::robin_map<Voxel, Eigen::Vector3d> grid;
grid.reserve(frame.size());
for (const auto &point : frame) {
std::for_each(frame.cbegin(), frame.cend(), [&](const auto &point) {
const auto voxel = PointToVoxel(point, voxel_size);
if (grid.contains(voxel)) continue;
grid.insert({voxel, point});
}
if (!grid.contains(voxel)) grid.insert({voxel, point});
nachovizzo marked this conversation as resolved.
Show resolved Hide resolved
});
std::vector<Eigen::Vector3d> frame_dowsampled;
frame_dowsampled.reserve(grid.size());
for (const auto &[voxel, point] : grid) {
(void)voxel;
frame_dowsampled.emplace_back(point);
}
std::for_each(grid.cbegin(), grid.cend(), [&](const auto &voxel_and_point) {
nachovizzo marked this conversation as resolved.
Show resolved Hide resolved
frame_dowsampled.emplace_back(voxel_and_point.second);
});
return frame_dowsampled;
}

Expand Down
6 changes: 3 additions & 3 deletions cpp/kiss_icp/core/Preprocessing.hpp
nachovizzo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <vector>

namespace kiss_icp {
/// Voxelize point cloud keeping the original coordinates
std::vector<Eigen::Vector3d> VoxelDownsample(const std::vector<Eigen::Vector3d> &frame,
double voxel_size);

/// Crop the frame with max/min ranges
std::vector<Eigen::Vector3d> Preprocess(const std::vector<Eigen::Vector3d> &frame,
Expand All @@ -39,7 +42,4 @@ std::vector<Eigen::Vector3d> Preprocess(const std::vector<Eigen::Vector3d> &fram
/// Originally introduced the calibration factor)
std::vector<Eigen::Vector3d> CorrectKITTIScan(const std::vector<Eigen::Vector3d> &frame);

/// Voxelize point cloud keeping the original coordinates
nachovizzo marked this conversation as resolved.
Show resolved Hide resolved
std::vector<Eigen::Vector3d> VoxelDownsample(const std::vector<Eigen::Vector3d> &frame,
double voxel_size);
} // namespace kiss_icp
4 changes: 2 additions & 2 deletions cpp/kiss_icp/core/Registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void TransformPoints(const Sophus::SE3d &T, std::vector<Eigen::Vector3d> &points
[&](const auto &point) { return T * point; });
}

using Voxel = kiss_icp::VoxelHashMap::Voxel;
using Voxel = kiss_icp::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) {
Expand All @@ -69,7 +69,7 @@ std::vector<Voxel> GetAdjacentVoxels(const Voxel &voxel, int adjacent_voxels = 1
std::tuple<Eigen::Vector3d, double> GetClosestNeighbor(const Eigen::Vector3d &point,
const kiss_icp::VoxelHashMap &voxel_map) {
// Convert the point to voxel coordinates
const auto &voxel = voxel_map.PointToVoxel(point);
const auto &voxel = kiss_icp::PointToVoxel(point, voxel_map.voxel_size_);
// Get nearby voxels on the map
const auto &query_voxels = GetAdjacentVoxels(voxel);
// Extract the points contained within the neighborhood voxels
Expand Down
2 changes: 1 addition & 1 deletion cpp/kiss_icp/core/VoxelHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void VoxelHashMap::Update(const std::vector<Eigen::Vector3d> &points, const Soph

void VoxelHashMap::AddPoints(const std::vector<Eigen::Vector3d> &points) {
std::for_each(points.cbegin(), points.cend(), [&](const auto &point) {
auto voxel = PointToVoxel(point);
auto voxel = PointToVoxel(point, voxel_size_);
tizianoGuadagnino marked this conversation as resolved.
Show resolved Hide resolved
auto search = map_.find(voxel);
if (search != map_.end()) {
auto &voxel_block = search.value();
Expand Down
17 changes: 3 additions & 14 deletions cpp/kiss_icp/core/VoxelHashMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
#include <sophus/se3.hpp>
#include <vector>

#include "VoxelUtils.hpp"

namespace kiss_icp {
struct VoxelHashMap {
using Voxel = Eigen::Vector3i;
struct VoxelBlock {
// buffer of points with a max limit of n_points
std::vector<Eigen::Vector3d> points;
Expand All @@ -43,25 +44,13 @@ struct VoxelHashMap {
if (points.size() < static_cast<size_t>(num_points_)) points.push_back(point);
}
};
struct VoxelHash {
size_t operator()(const Voxel &voxel) const {
const uint32_t *vec = reinterpret_cast<const uint32_t *>(voxel.data());
return (vec[0] * 73856093 ^ vec[1] * 19349669 ^ vec[2] * 83492791);
}
};

explicit VoxelHashMap(double voxel_size, double max_distance, int max_points_per_voxel)
: voxel_size_(voxel_size),
max_distance_(max_distance),
max_points_per_voxel_(max_points_per_voxel) {}

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>(std::floor(point.x() / voxel_size_)),
static_cast<int>(std::floor(point.y() / voxel_size_)),
static_cast<int>(std::floor(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);
Expand All @@ -72,6 +61,6 @@ struct VoxelHashMap {
double voxel_size_;
double max_distance_;
int max_points_per_voxel_;
tsl::robin_map<Voxel, VoxelBlock, VoxelHash> map_;
tsl::robin_map<Voxel, VoxelBlock> map_;
};
} // namespace kiss_icp
45 changes: 45 additions & 0 deletions cpp/kiss_icp/core/VoxelUtils.hpp
nachovizzo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// MIT License
//
// Copyright (c) 2024 Ignacio Vizzo, Tiziano Guadagnino, Benedikt Mersch, Cyrill
// Stachniss.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#pragma once

#include <Eigen/Core>
#include <cmath>
#include <memory>

tizianoGuadagnino marked this conversation as resolved.
Show resolved Hide resolved
namespace kiss_icp {
using Voxel = Eigen::Vector3i;
inline Voxel PointToVoxel(const Eigen::Vector3d &point, const double voxel_size) {
return Voxel(static_cast<int>(std::floor(point.x() / voxel_size)),
static_cast<int>(std::floor(point.y() / voxel_size)),
static_cast<int>(std::floor(point.z() / voxel_size)));
}
} // namespace kiss_icp

template <>
nachovizzo marked this conversation as resolved.
Show resolved Hide resolved
struct std::hash<kiss_icp::Voxel> {
std::size_t operator()(const kiss_icp::Voxel &voxel) const noexcept {
const uint32_t *vec = reinterpret_cast<const uint32_t *>(voxel.data());
return (vec[0] * 73856093 ^ vec[1] * 19349669 ^ vec[2] * 83492791);
}
};
Loading