Go to the documentation of this file.
7 #include <SimoxUtility/algorithm/string/string_tools.h>
23 for (
const auto& [_, num] : *
this)
32 const float total =
static_cast<float>(
getTotal());
34 std::map<Label, float> density;
35 for (
const auto& [label, num] : *
this)
37 density[label] = total > 0.f ? num / total : 0.f;;
49 return *std::max_element(begin(), end());
55 std::set<Label> labels;
56 for (
const Voxel* voxel :
61 for (
const auto& [label, num] : *voxel)
69 for (
Label label : labels)
71 if ((*this).at(label) != rhs.at(label))
81 return !(*
this == rhs);
87 std::vector<std::string> items;
88 for (
const auto& [label, num] : voxel)
92 os << simox::alg::join(items,
", ") <<
" }";
97 template <
class Po
intT>
98 static void addPointCloud(
100 const pcl::PointCloud<PointT>& _pointCloud,
106 _pointCloud, pointCloudPose, grid.
getPose());
109 const bool local =
true;
111 for (
const PointT& point : pointCloud)
113 const Eigen::Vector3f vpoint(point.data);
122 voxel[
static_cast<Label>(point.label)]++;
129 const pcl::PointCloud<pcl::PointXYZL>& pointCloud,
132 LabelDensity::addPointCloud(*
this, pointCloud, pointCloudPose);
136 const pcl::PointCloud<pcl::PointXYZRGBL>& pointCloud,
139 LabelDensity::addPointCloud(*
this, pointCloud, pointCloudPose);
146 for (
const Voxel& voxel : *
this)
148 for (
const auto& item : voxel)
150 ids.insert(item.first);
158 for (
auto& voxel : *
this)
160 std::set<Label> remove;
161 for (
const auto& [label, num] : voxel)
163 if (num < minNumPoints)
165 remove.insert(label);
169 for (
Label label : remove)
171 voxel.erase(voxel.find(label));
183 for (
Label label : labels)
187 const auto find = voxel.find(label);
188 return find != voxel.end() ? find->second : 0;
195 static io::VoxelAttributeSetterMap<Voxel> makeJsonSetters(
const std::set<Label>& labels)
197 voxelgrid::io::VoxelAttributeSetterMap<Voxel> setters;
198 for (
Label label : labels)
200 setters[
std::to_string(label)] = [label](
const nlohmann::json & j, Voxel & voxel)
208 static std::set<Label> getLabelsFromKeys(
const nlohmann::json& json)
212 std::set<Label> labels;
213 for (
const auto& item : json.items())
215 if (item.key() ==
"structure")
221 const int key = std::stoi(item.key());
224 labels.insert(
static_cast<Label>(key));
227 catch (std::invalid_argument&)
239 io::JsonIO::write<Voxel>(os, *
this, makeJsonGetters(*
this));
244 io::JsonIO::write<Voxel>(file, *
this, makeJsonGetters(*
this));
257 std::ifstream ifs(file);
275 static const std::string sep =
",";
281 std::vector<std::string> strings;
283 strings.push_back(
"index");
285 std::transform(labels.begin(), labels.end(), std::back_inserter(strings),
288 return std::to_string(l);
294 strings.push_back(
"total");
297 os << simox::alg::join(strings, sep) << std::endl;
310 std::transform(labels.begin(), labels.end(), std::back_inserter(strings),
311 [&voxel](
const Label id)
313 auto it = voxel.find(id);
314 return std::to_string(it != voxel.end() ? it->second : 0);
324 os << simox::alg::join(strings, sep) << std::endl;
331 std::ofstream os(file);
332 toCsv(os, includeTotal);
static void fromJson(const nlohmann::json &j, VoxelGrid< VoxelT > &grid, const VoxelAttributeSetterMap< VoxelT > &attributeMap)
Deserialize a voxel grid from JSON.
A 3D grid of voxels of type _VoxelT.
static void toJson(nlohmann::json &j, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Serialize a voxel grid to JSON.
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.
Label getMaxLabel() const
Get the label with most points.
Eigen::Matrix4f getPose() const
Get the grid pose in the world frame.
std::ostream & operator<<(std::ostream &os, const Voxel &voxel)
void toCsv(std::ostream &os, bool includeTotal=false) const
Write the voxel data to a CSV file.
std::map< std::string, VoxelAttributeGetter< VoxelT > > VoxelAttributeGetterMap
Map of attribute name to attribute getter.
void reduceNoise(std::size_t minNumPoints)
Remove entries with number of points below minNumPoints.
Voxel grid storing the number of points per label in each voxel.
bool empty(const std::string &s)
void readJson(std::istream &is)
Read this voxel grid from JSON.
void writeJson(std::ostream &os) const
Write this voxel grid to JSON.
VoxelT & getVoxel(std::size_t index)
Get the voxel with the given index.
bool isFree() const
Indicate whether this voxel is free, i.e. it contains no points.
uint32_t Label
Type of an object label.
std::size_t numVoxels() const
Get the number of voxels in the grid.
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
static nlohmann::json readJson(const std::string &filename)
Read JSON from file.
std::map< Label, float > getDensity() const
Get the relative frequency of labels, so that the sum is 1.0.
std::size_t getTotal() const
Get the total number of points in this voxel.
Eigen::Vector3i getVoxelGridIndex(size_t index) const
Get the grid index of the voxel with the given flat index.
std::pair< Label, std::size_t > getMax() const
Get the label with most points and the number of points.
Map of labels (object IDs) to number of points.
const std::string & to_string(const std::string &s)
std::set< Label > getUniqueLabels() const
Get the set of unique labels.
void addPointCloud(const pcl::PointCloud< pcl::PointXYZL > &pointCloud, const Eigen::Matrix4f &pointCloudPose=Eigen::Matrix4f::Identity())
Add the point cloud.
void from_json(const nlohmann::json &json, VoxelGrid &grid)
Read the voxel grid from JSON.
MatrixXX< 4, 4, float > Matrix4f
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
bool operator==(const Voxel &rhs) const
Equality operator.
bool isInside(const Eigen::Vector3i &index) const
Indicate whether the given point is inside the voxel.
void to_json(nlohmann::json &json, const VoxelGrid &grid)
Write the voxel grid to JSON.
bool operator!=(const Voxel &rhs) const