VoxelGrid.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <map>
5 #include <set>
6 
7 #include <Eigen/Core>
8 
9 #include <pcl/point_cloud.h>
10 #include <pcl/point_types.h>
11 
12 #include <SimoxUtility/json/json.hpp>
13 
15 
16 #include "../types.h"
17 
19 {
20 
21  // VOXEL TYPE
22 
23  /**
24  * @brief Map of labels (object IDs) to number of points.
25  */
26  class Voxel : public std::map<Label, std::size_t>
27  {
28  using Base = std::map<Label, std::size_t>;
29 
30  public:
31  using Base::map;
32 
33 
34  /// Indicate whether this voxel is free, i.e. it contains no points.
35  bool isFree() const;
36 
37  /// Get the total number of points in this voxel.
38  std::size_t getTotal() const;
39 
40  /// Get the relative frequency of labels, so that the sum is 1.0.
41  /// If `getTotal()` is 0, the total of the returned density is also 0.0.
42  std::map<Label, float> getDensity() const;
43 
44 
45  /// Get the label with most points.
46  Label getMaxLabel() const;
47 
48  /// Get the label with most points and the number of points.
49  std::pair<Label, std::size_t> getMax() const;
50 
51 
52  /// Equality operator.
53  /// Considers a non-existent label and a label with count 0 equal.
54  bool operator==(const Voxel& rhs) const;
55  bool operator!=(const Voxel& rhs) const;
56 
57  /// Outstream operator.
58  friend std::ostream& operator<<(std::ostream& os, const Voxel& voxel);
59  };
60 
61  // VOXEL GRID
62 
63  /**
64  * @brief Voxel grid storing the number of points per label in each voxel.
65  */
66  class VoxelGrid : public visionx::VoxelGrid<Voxel>
67  {
69 
70  public:
71  using Base::VoxelGrid;
72 
73 
74  /**
75  * @brief Add the point cloud.
76  * @param pointCloud The point cloud.
77  * @param pointCloudPose The point cloud pose (in the same frame as the grid's pose).
78  */
79  void addPointCloud(const pcl::PointCloud<pcl::PointXYZL>& pointCloud,
80  const Eigen::Matrix4f& pointCloudPose = Eigen::Matrix4f::Identity());
81  void addPointCloud(const pcl::PointCloud<pcl::PointXYZRGBL>& pointCloud,
82  const Eigen::Matrix4f& pointCloudPose = Eigen::Matrix4f::Identity());
83 
84 
85  /// Get the set of unique labels.
86  std::set<Label> getUniqueLabels() const;
87 
88 
89  /// Remove entries with number of points below `minNumPoints`.
90  void reduceNoise(std::size_t minNumPoints);
91 
92 
93  // Serialization
94 
95  /// Write this voxel grid to JSON.
96  void writeJson(std::ostream& os) const;
97  void writeJson(const std::filesystem::path& file) const;
98 
99  /// Read this voxel grid from JSON.
100  void readJson(std::istream& is);
101  void readJson(const std::filesystem::path& file);
102 
103 
104  /**
105  * @brief Write the voxel data to a CSV file. (Structure is not stored.)
106  *
107  * The first column, "index", specified the flat voxel index.
108  * The other columns represent the labels, and each row entry
109  * specifies the number of points with that label in the respective
110  * voxel.
111  *
112  * If `includeTotal` is true, a final column "total" is added, storing
113  * the voxel-wise total number of points.
114  */
115  void toCsv(std::ostream& os, bool includeTotal = false) const;
116  void toCsv(const std::filesystem::path& file, bool includeTotal = false) const;
117  };
118 
119  /// Write the voxel grid to JSON.
120  void to_json(nlohmann::json& json, const VoxelGrid& grid);
121  /// Read the voxel grid from JSON.
122  void from_json(const nlohmann::json& json, VoxelGrid& grid);
123 
124 
125 } // namespace visionx::voxelgrid::LabelDensity
visionx::voxelgrid::VoxelGrid< VoxelT >
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
visionx::voxelgrid::LabelDensity::Voxel::getMaxLabel
Label getMaxLabel() const
Get the label with most points.
Definition: VoxelGrid.cpp:46
visionx::voxelgrid::LabelDensity::VoxelGrid::toCsv
void toCsv(std::ostream &os, bool includeTotal=false) const
Write the voxel data to a CSV file.
Definition: VoxelGrid.cpp:280
visionx::voxelgrid::LabelDensity::VoxelGrid::reduceNoise
void reduceNoise(std::size_t minNumPoints)
Remove entries with number of points below minNumPoints.
Definition: VoxelGrid.cpp:159
visionx::voxelgrid::LabelDensity::VoxelGrid
Voxel grid storing the number of points per label in each voxel.
Definition: VoxelGrid.h:66
visionx::voxelgrid::LabelDensity::VoxelGrid::readJson
void readJson(std::istream &is)
Read this voxel grid from JSON.
Definition: VoxelGrid.cpp:252
visionx::voxelgrid::LabelDensity::VoxelGrid::writeJson
void writeJson(std::ostream &os) const
Write this voxel grid to JSON.
Definition: VoxelGrid.cpp:240
visionx::voxelgrid::LabelDensity::Voxel::isFree
bool isFree() const
Indicate whether this voxel is free, i.e. it contains no points.
Definition: VoxelGrid.cpp:15
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:6
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
visionx::voxelgrid::LabelDensity::Voxel::operator<<
friend std::ostream & operator<<(std::ostream &os, const Voxel &voxel)
Outstream operator.
Definition: VoxelGrid.cpp:89
visionx::voxelgrid::LabelDensity::Voxel::getDensity
std::map< Label, float > getDensity() const
Get the relative frequency of labels, so that the sum is 1.0.
Definition: VoxelGrid.cpp:32
visionx::voxelgrid::LabelDensity::Voxel::getTotal
std::size_t getTotal() const
Get the total number of points in this voxel.
Definition: VoxelGrid.cpp:21
visionx::voxelgrid::LabelDensity::Voxel::getMax
std::pair< Label, std::size_t > getMax() const
Get the label with most points and the number of points.
Definition: VoxelGrid.cpp:52
VoxelGridCore.h
visionx::voxelgrid::LabelDensity::Voxel
Map of labels (object IDs) to number of points.
Definition: VoxelGrid.h:26
visionx::voxelgrid::LabelDensity::VoxelGrid::getUniqueLabels
std::set< Label > getUniqueLabels() const
Get the set of unique labels.
Definition: VoxelGrid.cpp:145
visionx::voxelgrid::LabelDensity::VoxelGrid::addPointCloud
void addPointCloud(const pcl::PointCloud< pcl::PointXYZL > &pointCloud, const Eigen::Matrix4f &pointCloudPose=Eigen::Matrix4f::Identity())
Add the point cloud.
Definition: VoxelGrid.cpp:131
visionx::voxelgrid::LabelDensity::from_json
void from_json(const nlohmann::json &json, VoxelGrid &grid)
Read the voxel grid from JSON.
Definition: VoxelGrid.cpp:274
visionx::voxelgrid::LabelDensity::Voxel::operator==
bool operator==(const Voxel &rhs) const
Equality operator.
Definition: VoxelGrid.cpp:58
visionx::VoxelGrid
voxelgrid::VoxelGrid< VoxelT > VoxelGrid
Definition: VoxelGridCore.h:34
visionx::voxelgrid::LabelDensity::to_json
void to_json(nlohmann::json &json, const VoxelGrid &grid)
Write the voxel grid to JSON.
Definition: VoxelGrid.cpp:268
visionx::voxelgrid::LabelDensity
Definition: Visualizer.cpp:8
visionx::voxelgrid::LabelDensity::Voxel::operator!=
bool operator!=(const Voxel &rhs) const
Definition: VoxelGrid.cpp:83