PointCloudToVoxelGridSingle.h
Go to the documentation of this file.
1 /* -------------------------------------------------------------------------
2  * A repertory of multi primitive-to-primitive (MP2P) ICP algorithms in C++
3  * Copyright (C) 2018-2024 Jose Luis Blanco, University of Almeria
4  * See LICENSE for license information.
5  * ------------------------------------------------------------------------- */
13 #pragma once
14 
15 #include <mrpt/core/pimpl.h>
16 #include <mrpt/maps/CPointsMap.h>
17 
18 #include <optional>
19 
21 namespace mp2p_icp_filters
22 {
29 {
30  public:
33 
35  void setResolution(const float voxel_size);
36 
37  void processPointCloud(const mrpt::maps::CPointsMap& p);
38 
41  void clear();
42 
44  struct voxel_t
45  {
46  std::optional<mrpt::math::TPoint3Df> point;
47  std::optional<size_t> pointIdx; // Index in the source
48  std::optional<const mrpt::maps::CPointsMap*> source;
49 
51  uint32_t pointCount = 0;
52  };
53 
54  struct indices_t
55  {
56  indices_t(int32_t cx, int32_t cy, int32_t cz)
57  : cx_(cx), cy_(cy), cz_(cz)
58  {
59  }
60 
61  int32_t cx_ = 0, cy_ = 0, cz_ = 0;
62 
63  bool operator==(const indices_t& o) const
64  {
65  return cx_ == o.cx_ && cy_ == o.cy_ && cz_ == o.cz_;
66  }
67  };
68 
76  struct IndicesHash
77  {
79  std::size_t operator()(const indices_t& k) const noexcept
80  {
81  // These are the implicit assumptions of the reinterpret cast below:
82  static_assert(sizeof(indices_t::cx_) == sizeof(uint32_t));
83  static_assert(offsetof(indices_t, cx_) == 0 * sizeof(uint32_t));
84  static_assert(offsetof(indices_t, cy_) == 1 * sizeof(uint32_t));
85  static_assert(offsetof(indices_t, cz_) == 2 * sizeof(uint32_t));
86 
87  const uint32_t* vec = reinterpret_cast<const uint32_t*>(&k);
88  return ((1 << 20) - 1) &
89  (vec[0] * 73856093 ^ vec[1] * 19349663 ^ vec[2] * 83492791);
90  }
91 
92  // k1 < k2?
93  bool operator()(const indices_t& k1, const indices_t& k2) const noexcept
94  {
95  if (k1.cx_ != k2.cx_) return k1.cx_ < k2.cx_;
96  if (k1.cy_ != k2.cy_) return k1.cy_ < k2.cy_;
97  return k1.cz_ < k2.cz_;
98  }
99  };
100 
101  inline int32_t coord2idx(float xyz) const
102  {
103  return static_cast<int32_t>(xyz / resolution_);
104  }
105 
106  void visit_voxels(
107  const std::function<void(const indices_t idx, const voxel_t& vxl)>&
108  userCode) const;
109 
111  size_t size() const;
112 
113  private:
115  float resolution_ = 0.20f;
116 
119  struct Impl;
120  mrpt::pimpl<Impl> impl_;
121 };
122 
123 } // namespace mp2p_icp_filters
mp2p_icp_filters::PointCloudToVoxelGridSingle::size
size_t size() const
Returns the number of occupied voxels.
Definition: PointCloudToVoxelGridSingle.cpp:96
mp2p_icp_filters::PointCloudToVoxelGridSingle::indices_t::cz_
int32_t cz_
Definition: PointCloudToVoxelGridSingle.h:61
mp2p_icp_filters::PointCloudToVoxelGridSingle::coord2idx
int32_t coord2idx(float xyz) const
Definition: PointCloudToVoxelGridSingle.h:101
mp2p_icp_filters::PointCloudToVoxelGridSingle::processPointCloud
void processPointCloud(const mrpt::maps::CPointsMap &p)
Definition: PointCloudToVoxelGridSingle.cpp:39
mp2p_icp_filters::PointCloudToVoxelGridSingle::indices_t::operator==
bool operator==(const indices_t &o) const
Definition: PointCloudToVoxelGridSingle.h:63
mp2p_icp_filters::PointCloudToVoxelGridSingle::clear
void clear()
Definition: PointCloudToVoxelGridSingle.cpp:82
mp2p_icp_filters::PointCloudToVoxelGridSingle::voxel_t::pointCount
uint32_t pointCount
Definition: PointCloudToVoxelGridSingle.h:51
mp2p_icp_filters::PointCloudToVoxelGridSingle::voxel_t::point
std::optional< mrpt::math::TPoint3Df > point
Definition: PointCloudToVoxelGridSingle.h:46
mp2p_icp_filters::PointCloudToVoxelGridSingle::indices_t::cx_
int32_t cx_
Definition: PointCloudToVoxelGridSingle.h:61
mp2p_icp_filters::PointCloudToVoxelGridSingle::IndicesHash::operator()
bool operator()(const indices_t &k1, const indices_t &k2) const noexcept
Definition: PointCloudToVoxelGridSingle.h:93
mp2p_icp_filters::PointCloudToVoxelGridSingle::indices_t::indices_t
indices_t(int32_t cx, int32_t cy, int32_t cz)
Definition: PointCloudToVoxelGridSingle.h:56
mp2p_icp_filters::PointCloudToVoxelGridSingle::indices_t::cy_
int32_t cy_
Definition: PointCloudToVoxelGridSingle.h:61
mp2p_icp_filters::PointCloudToVoxelGridSingle::~PointCloudToVoxelGridSingle
~PointCloudToVoxelGridSingle()
Definition: PointCloudToVoxelGridSingle.h:32
mp2p_icp_filters::PointCloudToVoxelGridSingle::visit_voxels
void visit_voxels(const std::function< void(const indices_t idx, const voxel_t &vxl)> &userCode) const
Definition: PointCloudToVoxelGridSingle.cpp:89
mp2p_icp_filters::PointCloudToVoxelGridSingle::voxel_t
Definition: PointCloudToVoxelGridSingle.h:44
mp2p_icp_filters::PointCloudToVoxelGridSingle::Impl
Definition: PointCloudToVoxelGridSingle.cpp:19
mp2p_icp_filters::PointCloudToVoxelGridSingle::IndicesHash
Definition: PointCloudToVoxelGridSingle.h:76
mp2p_icp_filters::PointCloudToVoxelGridSingle
Definition: PointCloudToVoxelGridSingle.h:28
mp2p_icp_filters::PointCloudToVoxelGridSingle::resolution_
float resolution_
Definition: PointCloudToVoxelGridSingle.h:115
mp2p_icp_filters::PointCloudToVoxelGridSingle::PointCloudToVoxelGridSingle
PointCloudToVoxelGridSingle()
Definition: PointCloudToVoxelGridSingle.cpp:24
mp2p_icp_filters::PointCloudToVoxelGridSingle::voxel_t::pointIdx
std::optional< size_t > pointIdx
Definition: PointCloudToVoxelGridSingle.h:47
mp2p_icp_filters::PointCloudToVoxelGridSingle::setResolution
void setResolution(const float voxel_size)
Definition: PointCloudToVoxelGridSingle.cpp:29
mp2p_icp_filters::PointCloudToVoxelGridSingle::indices_t
Definition: PointCloudToVoxelGridSingle.h:54
mp2p_icp_filters::PointCloudToVoxelGridSingle::impl_
mrpt::pimpl< Impl > impl_
Definition: PointCloudToVoxelGridSingle.h:119
mp2p_icp_filters::PointCloudToVoxelGridSingle::IndicesHash::operator()
std::size_t operator()(const indices_t &k) const noexcept
Hash operator for unordered maps:
Definition: PointCloudToVoxelGridSingle.h:79
mp2p_icp_filters::PointCloudToVoxelGridSingle::voxel_t::source
std::optional< const mrpt::maps::CPointsMap * > source
Definition: PointCloudToVoxelGridSingle.h:48
mp2p_icp_filters
Definition: FilterAdjustTimestamps.h:19


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:45:59