small_gicp
gaussian_voxelmap.hpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright 2024 Kenji Koide
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 
5 #include <Eigen/Core>
6 #include <Eigen/Geometry>
7 
11 
12 namespace small_gicp {
13 
15 struct GaussianVoxel {
16 public:
17  struct Setting {};
18 
20  GaussianVoxel() : finalized(false), num_points(0), mean(Eigen::Vector4d::Zero()), cov(Eigen::Matrix4d::Zero()) {}
21 
23  size_t size() const { return 1; }
24 
31  template <typename PointCloud>
32  void add(const Setting& setting, const Eigen::Vector4d& transformed_pt, const PointCloud& points, size_t i, const Eigen::Isometry3d& T) {
33  if (finalized) {
34  this->finalized = false;
35  this->mean *= num_points;
36  this->cov *= num_points;
37  }
38 
39  num_points++;
40  this->mean += transformed_pt;
41  this->cov += T.matrix() * traits::cov(points, i) * T.matrix().transpose();
42  }
43 
45  void finalize() {
46  if (finalized) {
47  return;
48  }
49 
50  mean /= num_points;
51  cov /= num_points;
52  finalized = true;
53  }
54 
55 public:
56  bool finalized;
57  size_t num_points;
58  Eigen::Vector4d mean;
59  Eigen::Matrix4d cov;
60 };
61 
62 namespace traits {
63 
64 template <>
66  static size_t size(const GaussianVoxel& voxel) { return 1; }
67  static bool has_points(const GaussianVoxel& voxel) { return true; }
68  static bool has_covs(const GaussianVoxel& voxel) { return true; }
69 
70  static const Eigen::Vector4d& point(const GaussianVoxel& voxel, size_t i) { return voxel.mean; }
71  static const Eigen::Matrix4d& cov(const GaussianVoxel& voxel, size_t i) { return voxel.cov; }
72 
73  static size_t nearest_neighbor_search(const GaussianVoxel& voxel, const Eigen::Vector4d& pt, size_t* k_index, double* k_sq_dist) {
74  *k_index = 0;
75  *k_sq_dist = (voxel.mean - pt).squaredNorm();
76  return 1;
77  }
78 
79  static size_t knn_search(const GaussianVoxel& voxel, const Eigen::Vector4d& pt, size_t k, size_t* k_index, double* k_sq_dist) {
80  return nearest_neighbor_search(voxel, pt, k_index, k_sq_dist);
81  }
82 
83  template <typename Result>
84  static void knn_search(const GaussianVoxel& voxel, const Eigen::Vector4d& pt, Result& result) {
85  result.push(0, (voxel.mean - pt).squaredNorm());
86  }
87 };
88 
89 } // namespace traits
90 
92 
93 } // namespace small_gicp
size_t nearest_neighbor_search(const T &tree, const Eigen::Vector4d &point, size_t *k_index, double *k_sq_dist)
Find the nearest neighbor. If Traits<T>::nearest_neighbor_search is not defined, fallback to knn_sear...
Definition: traits.hpp:44
auto cov(const T &points, size_t i)
Get i-th covariance. Only the top-left 3x3 matrix is filled, and the bottom row and the right col mus...
Definition: traits.hpp:52
Definition: flat_container.hpp:12
Definition: gaussian_voxelmap.hpp:17
Gaussian voxel that computes and stores voxel mean and covariance.
Definition: gaussian_voxelmap.hpp:15
Eigen::Matrix4d cov
Covariance.
Definition: gaussian_voxelmap.hpp:59
Eigen::Vector4d mean
Mean.
Definition: gaussian_voxelmap.hpp:58
void finalize()
Finalize the voxel mean and covariance.
Definition: gaussian_voxelmap.hpp:45
void add(const Setting &setting, const Eigen::Vector4d &transformed_pt, const PointCloud &points, size_t i, const Eigen::Isometry3d &T)
Add a point to the voxel.
Definition: gaussian_voxelmap.hpp:32
GaussianVoxel()
Constructor.
Definition: gaussian_voxelmap.hpp:20
size_t size() const
Number of points in the voxel (Always 1 for GaussianVoxel).
Definition: gaussian_voxelmap.hpp:23
bool finalized
If true, mean and cov are finalized, otherwise they represent the sum of input points.
Definition: gaussian_voxelmap.hpp:56
size_t num_points
Number of input points.
Definition: gaussian_voxelmap.hpp:57
Incremental voxelmap. This class supports incremental point cloud insertion and LRU-based voxel delet...
Definition: incremental_voxelmap.hpp:38
Point cloud.
Definition: point_cloud.hpp:15
static size_t nearest_neighbor_search(const GaussianVoxel &voxel, const Eigen::Vector4d &pt, size_t *k_index, double *k_sq_dist)
Definition: gaussian_voxelmap.hpp:73
static const Eigen::Vector4d & point(const GaussianVoxel &voxel, size_t i)
Definition: gaussian_voxelmap.hpp:70
static bool has_covs(const GaussianVoxel &voxel)
Definition: gaussian_voxelmap.hpp:68
static void knn_search(const GaussianVoxel &voxel, const Eigen::Vector4d &pt, Result &result)
Definition: gaussian_voxelmap.hpp:84
static size_t knn_search(const GaussianVoxel &voxel, const Eigen::Vector4d &pt, size_t k, size_t *k_index, double *k_sq_dist)
Definition: gaussian_voxelmap.hpp:79
static const Eigen::Matrix4d & cov(const GaussianVoxel &voxel, size_t i)
Definition: gaussian_voxelmap.hpp:71
static size_t size(const GaussianVoxel &voxel)
Definition: gaussian_voxelmap.hpp:66
static bool has_points(const GaussianVoxel &voxel)
Definition: gaussian_voxelmap.hpp:67
Definition: traits.hpp:13