Skip to content

Commit

Permalink
Revise Voxel functionalities in VoxelHashMap and Preprocessing (#362)
Browse files Browse the repository at this point in the history
* Unify Voxel functionalities, now everything comes from VoxelHashMap

* This looks like an even better unification

* Modernize VoxelDownsample

* VoxelUtils is born, now everthing is liteally in one place, and we have
some side effect code reduction

* Macos does not like my include

* Local style include

* With move

* As of benchmark

* For each for the win

* Remove unnecessary include

* Const correcteness

* Refresh changes

---------

Co-authored-by: Luca Lobefaro <[email protected]>
Co-authored-by: Ignacio Vizzo <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2024
1 parent 70c3e8d commit a9f527d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 31 deletions.
17 changes: 1 addition & 16 deletions cpp/kiss_icp/core/Preprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,7 @@
#include <cmath>
#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,
Expand Down
5 changes: 3 additions & 2 deletions cpp/kiss_icp/core/Registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <tuple>

#include "VoxelHashMap.hpp"
#include "VoxelUtils.hpp"

namespace Eigen {
using Matrix6d = Eigen::Matrix<double, 6, 6>;
Expand All @@ -54,7 +55,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 @@ -70,7 +71,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
4 changes: 3 additions & 1 deletion cpp/kiss_icp/core/VoxelHashMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <sophus/se3.hpp>
#include <vector>

#include "VoxelUtils.hpp"

namespace kiss_icp {

std::vector<Eigen::Vector3d> VoxelHashMap::GetPoints(const std::vector<Voxel> &query_voxels) const {
Expand Down Expand Up @@ -74,7 +76,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_);
auto search = map_.find(voxel);
if (search != map_.end()) {
auto &voxel_block = search.value();
Expand Down
14 changes: 2 additions & 12 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,12 +44,6 @@ 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),
Expand All @@ -57,11 +52,6 @@ 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>(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 Down
45 changes: 45 additions & 0 deletions cpp/kiss_icp/core/VoxelUtils.hpp
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>

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)));
}

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);
}
};
} // namespace kiss_icp

0 comments on commit a9f527d

Please sign in to comment.