5#include <SimoxUtility/algorithm/string/string_tools.h>
24 for (
const auto& [_, num] : *
this)
31 std::map<Label, float>
34 const float total =
static_cast<float>(
getTotal());
36 std::map<Label, float> density;
37 for (
const auto& [label, num] : *
this)
39 density[label] = total > 0.f ? num / total : 0.f;
51 std::pair<Label, std::size_t>
54 return *std::max_element(begin(), end());
61 std::set<Label> labels;
62 for (
const Voxel* voxel : {
this, &rhs})
64 for (
const auto& [label, num] : *voxel)
72 for (
Label label : labels)
74 if ((*this).at(label) != rhs.at(label))
85 return !(*
this == rhs);
92 std::vector<std::string> items;
93 for (
const auto& [label, num] : voxel)
95 items.push_back(std::to_string(label) +
": " + std::to_string(num));
97 os << simox::alg::join(items,
", ") <<
" }";
101 template <
class Po
intT>
104 const pcl::PointCloud<PointT>& _pointCloud,
105 const Eigen::Matrix4f& pointCloudPose)
109 const pcl::PointCloud<PointT> pointCloud =
113 const bool local =
true;
115 for (
const PointT& point : pointCloud)
117 const Eigen::Vector3f vpoint(point.data);
125 auto& voxel = grid.
getVoxel(indices);
126 voxel[
static_cast<Label>(point.label)]++;
131 VoxelGrid::addPointCloud(
const pcl::PointCloud<pcl::PointXYZL>& pointCloud,
132 const Eigen::Matrix4f& pointCloudPose)
134 LabelDensity::addPointCloud(*
this, pointCloud, pointCloudPose);
138 VoxelGrid::addPointCloud(
const pcl::PointCloud<pcl::PointXYZRGBL>& pointCloud,
139 const Eigen::Matrix4f& pointCloudPose)
141 LabelDensity::addPointCloud(*
this, pointCloud, pointCloudPose);
145 VoxelGrid::getUniqueLabels()
const
148 for (
const Voxel& voxel : *
this)
150 for (
const auto& item : voxel)
152 ids.insert(item.first);
159 VoxelGrid::reduceNoise(std::size_t minNumPoints)
161 for (
auto& voxel : *
this)
163 std::set<Label> remove;
164 for (
const auto& [label, num] : voxel)
166 if (num < minNumPoints)
168 remove.insert(label);
172 for (
Label label : remove)
174 voxel.erase(voxel.find(label));
186 for (
Label label : labels)
188 getters[std::to_string(label)] = [label](
const Voxel& voxel) -> nlohmann::json
190 const auto find = voxel.find(label);
191 return find != voxel.end() ? find->second : 0;
199 makeJsonSetters(
const std::set<Label>& labels)
202 for (
Label label : labels)
204 setters[std::to_string(label)] = [label](
const nlohmann::json& j, Voxel& voxel)
205 { voxel[label] = j; };
210 static std::set<Label>
211 getLabelsFromKeys(
const nlohmann::json& json)
215 std::set<Label> labels;
216 for (
const auto& item : json.items())
218 if (item.key() ==
"structure")
224 const int key = std::stoi(item.key());
227 labels.insert(
static_cast<Label>(key));
230 catch (std::invalid_argument&)
240 VoxelGrid::writeJson(std::ostream& os)
const
246 VoxelGrid::writeJson(
const std::filesystem::path& file)
const
252 VoxelGrid::readJson(std::istream& is)
261 VoxelGrid::readJson(
const std::filesystem::path& file)
263 std::ifstream ifs(file);
280 VoxelGrid::toCsv(std::ostream& os,
bool includeTotal)
const
282 static const std::string sep =
",";
288 std::vector<std::string> strings;
290 strings.push_back(
"index");
292 std::transform(labels.begin(),
294 std::back_inserter(strings),
295 [](
Label l) { return std::to_string(l); });
300 strings.push_back(
"total");
303 os << simox::alg::join(strings, sep) << std::endl;
314 strings.push_back(std::to_string(
index));
316 std::transform(labels.begin(),
318 std::back_inserter(strings),
319 [&voxel](
const Label id)
321 auto it = voxel.find(id);
322 return std::to_string(it != voxel.end() ? it->second : 0);
328 strings.push_back(std::to_string(voxel.
getTotal()));
332 os << simox::alg::join(strings, sep) << std::endl;
338 VoxelGrid::toCsv(
const std::filesystem::path& file,
bool includeTotal)
const
340 std::ofstream os(file);
341 toCsv(os, includeTotal);
int Label(int n[], int size, int *curLabel, MiscLib::Vector< std::pair< int, size_t > > *labels)
Voxel grid storing the number of points per label in each voxel.
std::set< Label > getUniqueLabels() const
Get the set of unique labels.
void toCsv(std::ostream &os, bool includeTotal=false) const
Write the voxel data to a CSV file.
void readJson(std::istream &is)
Read this voxel grid from JSON.
Map of labels (object IDs) to number of points.
std::pair< Label, std::size_t > getMax() const
Get the label with most points and the number of points.
std::size_t getTotal() const
Get the total number of points in this voxel.
std::map< Label, float > getDensity() const
Get the relative frequency of labels, so that the sum is 1.0.
bool operator==(const Voxel &rhs) const
Equality operator.
bool isFree() const
Indicate whether this voxel is free, i.e. it contains no points.
Label getMaxLabel() const
Get the label with most points.
bool operator!=(const Voxel &rhs) const
VoxelT & getVoxel(std::size_t index)
Get the voxel with the given index.
std::size_t numVoxels() const
bool isInside(const Eigen::Vector3i &index) const
Indicate whether the given point is inside the voxel.
Eigen::Vector3i getVoxelGridIndex(size_t index) const
Get the grid index of the voxel with the given flat index.
Eigen::Matrix4f getPose() const
Get the grid pose in the world frame.
static void write(const std::string &filename, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap, int indent=-1, char indentChar=' ')
Write a voxel grid to file.
static void toJson(nlohmann::json &j, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Serialize a voxel grid to JSON.
static void fromJson(const nlohmann::json &j, VoxelGrid< VoxelT > &grid, const VoxelAttributeSetterMap< VoxelT > &attributeMap)
Deserialize a voxel grid from JSON.
static nlohmann::json readJson(const std::string &filename)
Read JSON from file.
void from_json(const nlohmann::json &json, VoxelGrid &grid)
Read the voxel grid from JSON.
std::ostream & operator<<(std::ostream &os, const Voxel &voxel)
void to_json(nlohmann::json &json, const VoxelGrid &grid)
Write the voxel grid to JSON.
std::map< std::string, VoxelAttributeGetter< VoxelT > > VoxelAttributeGetterMap
Map of attribute name to attribute getter.
std::map< std::string, VoxelAttributeSetter< VoxelT > > VoxelAttributeSetterMap
Map of attribute name to attribute setter.
pcl::PointCloud< PointT > transformPointCloudToGrid(const pcl::PointCloud< PointT > &pointCloud, const Eigen::Matrix4f &pointCloudPose, const Eigen::Matrix4f &gridPose)
Transform a point cloud to local grid coordinates.
uint32_t Label
Type of an object label.