gtsam_points
Loading...
Searching...
No Matches
incremental_voxelmap.hpp
1// SPDX-FileCopyrightText: Copyright 2024 Kenji Koide
2// SPDX-License-Identifier: MIT
3#pragma once
4
5#include <Eigen/Core>
6#include <gtsam_points/util/vector3i_hash.hpp>
7#include <gtsam_points/types/point_cloud_cpu.hpp>
8#include <gtsam_points/ann/nearest_neighbor_search.hpp>
9
10namespace gtsam_points {
11
13struct VoxelInfo {
14public:
16 VoxelInfo() : lru(0), coord(-1, -1, -1) {}
17
21 VoxelInfo(const Eigen::Vector3i& coord, size_t lru) : lru(lru), coord(coord) {}
22
23public:
24 size_t lru;
25 Eigen::Vector3i coord;
26};
27
34template <typename VoxelContents>
36public:
37 using Ptr = std::shared_ptr<IncrementalVoxelMap>;
38 using ConstPtr = std::shared_ptr<const IncrementalVoxelMap>;
39
42 explicit IncrementalVoxelMap(double leaf_size);
43 virtual ~IncrementalVoxelMap();
44
46 void set_voxel_resolution(const double leaf_size) { inv_leaf_size = 1.0 / leaf_size; }
48 void set_lru_clear_cycle(const int lru_clear_cycle) { this->lru_clear_cycle = lru_clear_cycle; }
50 void set_lru_horizon(const int lru_horizon) { this->lru_horizon = lru_horizon; }
52 void set_neighbor_voxel_mode(const int mode) { offsets = neighbor_offsets(mode); }
54 typename VoxelContents::Setting& voxel_insertion_setting() { return voxel_setting; }
55
57 double leaf_size() const { return 1.0 / inv_leaf_size; }
58
60 size_t num_voxels() const { return flat_voxels.size(); }
61
63 virtual void clear();
64
68 virtual void insert(const PointCloud& points);
69
76 virtual size_t knn_search(
77 const double* pt,
78 size_t k,
79 size_t* k_indices,
80 double* k_sq_dists,
81 double max_sq_dist = std::numeric_limits<double>::max()) const override;
82
84 inline size_t calc_index(const size_t voxel_id, const size_t point_id) const { return (voxel_id << point_id_bits) | point_id; }
85 inline size_t voxel_id(const size_t i) const { return i >> point_id_bits; }
86 inline size_t point_id(const size_t i) const { return i & ((1ull << point_id_bits) - 1); }
87
88 bool has_points() const;
89 bool has_normals() const;
90 bool has_covs() const;
91 bool has_intensities() const;
92
93 decltype(auto) point(const size_t i) const { return frame::point(flat_voxels[voxel_id(i)]->second, point_id(i)); }
94 decltype(auto) normal(const size_t i) const { return frame::normal(flat_voxels[voxel_id(i)]->second, point_id(i)); }
95 decltype(auto) cov(const size_t i) const { return frame::cov(flat_voxels[voxel_id(i)]->second, point_id(i)); }
96 decltype(auto) intensity(const size_t i) const { return frame::intensity(flat_voxels[voxel_id(i)]->second, point_id(i)); }
97
98 virtual std::vector<Eigen::Vector4d> voxel_points() const;
99 virtual std::vector<Eigen::Vector4d> voxel_normals() const;
100 virtual std::vector<Eigen::Matrix4d> voxel_covs() const;
101 virtual std::vector<double> voxel_intensities() const;
102
103 virtual PointCloudCPU::Ptr voxel_data() const;
104
105protected:
106 std::vector<Eigen::Vector3i> neighbor_offsets(const int neighbor_voxel_mode) const;
107
108 template <typename Func>
109 void visit_points(const Func& f) const {
110 for (const auto& voxel : flat_voxels) {
111 for (int i = 0; i < frame::size(voxel->second); i++) {
112 f(voxel->second, i);
113 }
114 }
115 }
116
117protected:
118 static_assert(sizeof(size_t) == 8, "size_t must be 64-bit");
119 static constexpr int point_id_bits = 32;
120 static constexpr int voxel_id_bits = 64 - point_id_bits;
122 std::vector<Eigen::Vector3i> offsets;
123
124 size_t lru_horizon;
126 size_t lru_counter;
127
128 typename VoxelContents::Setting voxel_setting;
129 std::vector<std::shared_ptr<std::pair<VoxelInfo, VoxelContents>>> flat_voxels;
130 std::unordered_map<Eigen::Vector3i, size_t, XORVector3iHash> voxels;
131};
132
133namespace frame {
134
135template <typename VoxelContents>
136struct traits<IncrementalVoxelMap<VoxelContents>> {
137 static bool has_points(const IncrementalVoxelMap<VoxelContents>& ivox) { return ivox.has_points(); }
138 static bool has_normals(const IncrementalVoxelMap<VoxelContents>& ivox) { return ivox.has_normals(); }
139 static bool has_covs(const IncrementalVoxelMap<VoxelContents>& ivox) { return ivox.has_covs(); }
140 static bool has_intensities(const IncrementalVoxelMap<VoxelContents>& ivox) { return ivox.has_intensities(); }
141
142 static const Eigen::Vector4d& point(const IncrementalVoxelMap<VoxelContents>& ivox, size_t i) { return ivox.point(i); }
143 static const Eigen::Vector4d& normal(const IncrementalVoxelMap<VoxelContents>& ivox, size_t i) { return ivox.normal(i); }
144 static const Eigen::Matrix4d& cov(const IncrementalVoxelMap<VoxelContents>& ivox, size_t i) { return ivox.cov(i); }
145 static double intensity(const IncrementalVoxelMap<VoxelContents>& ivox, size_t i) { return ivox.intensity(i); }
146};
147
148} // namespace frame
149
150} // namespace gtsam_points
Incremental voxelmap. This class supports incremental point cloud insertion and LRU-based voxel delet...
Definition incremental_voxelmap.hpp:35
double inv_leaf_size
Inverse of the voxel size.
Definition incremental_voxelmap.hpp:121
size_t num_voxels() const
Number of voxels in the voxelmap.
Definition incremental_voxelmap.hpp:60
size_t point_id(const size_t i) const
Extract the voxel ID from a global index.
Definition incremental_voxelmap.hpp:86
virtual void clear()
Clear the voxelmap.
Definition incremental_voxelmap_impl.hpp:24
std::vector< Eigen::Vector3i > offsets
Neighbor voxel offsets.
Definition incremental_voxelmap.hpp:122
void set_neighbor_voxel_mode(const int mode)
Neighboring voxel search mode (1, 7, 19, or 27).
Definition incremental_voxelmap.hpp:52
size_t voxel_id(const size_t i) const
Extract the point ID from a global index.
Definition incremental_voxelmap.hpp:85
VoxelContents::Setting & voxel_insertion_setting()
Voxel setting.
Definition incremental_voxelmap.hpp:54
void set_lru_horizon(const int lru_horizon)
LRU cache horizon.
Definition incremental_voxelmap.hpp:50
void set_voxel_resolution(const double leaf_size)
Voxel resolution.
Definition incremental_voxelmap.hpp:46
size_t lru_counter
LRU counter. Incremented every step.
Definition incremental_voxelmap.hpp:126
virtual void insert(const PointCloud &points)
Insert points to the voxelmap.
Definition incremental_voxelmap_impl.hpp:31
std::unordered_map< Eigen::Vector3i, size_t, XORVector3iHash > voxels
Voxel index map.
Definition incremental_voxelmap.hpp:130
static constexpr int point_id_bits
Use the first 32 bits for point id.
Definition incremental_voxelmap.hpp:119
size_t lru_horizon
LRU horizon size. Voxels that have not been accessed for lru_horizon steps are deleted.
Definition incremental_voxelmap.hpp:124
VoxelContents::Setting voxel_setting
Voxel setting.
Definition incremental_voxelmap.hpp:128
double leaf_size() const
Voxel size.
Definition incremental_voxelmap.hpp:57
size_t calc_index(const size_t voxel_id, const size_t point_id) const
Calculate the global point index from the voxel index and the point index.
Definition incremental_voxelmap.hpp:84
size_t lru_clear_cycle
LRU clear cycle. Voxel deletion is performed every lru_clear_cycle steps.
Definition incremental_voxelmap.hpp:125
void set_lru_clear_cycle(const int lru_clear_cycle)
LRU cache clearing cycle.
Definition incremental_voxelmap.hpp:48
virtual size_t knn_search(const double *pt, size_t k, size_t *k_indices, double *k_sq_dists, double max_sq_dist=std::numeric_limits< double >::max()) const override
Find k nearest neighbors.
Definition incremental_voxelmap_impl.hpp:71
std::vector< std::shared_ptr< std::pair< VoxelInfo, VoxelContents > > > flat_voxels
Voxel contents.
Definition incremental_voxelmap.hpp:129
static constexpr int voxel_id_bits
Use the remaining bits for voxel id.
Definition incremental_voxelmap.hpp:120
Nearest neighbor search interface.
Definition nearest_neighbor_search.hpp:16
Standard point cloud class that holds only pointers to point attributes.
Definition point_cloud.hpp:19
Voxel meta information.
Definition incremental_voxelmap.hpp:13
VoxelInfo()
Default constructor.
Definition incremental_voxelmap.hpp:16
Eigen::Vector3i coord
Voxel coordinate.
Definition incremental_voxelmap.hpp:25
VoxelInfo(const Eigen::Vector3i &coord, size_t lru)
Constructor.
Definition incremental_voxelmap.hpp:21
size_t lru
Last used time.
Definition incremental_voxelmap.hpp:24
Definition frame_traits.hpp:17