Go to the documentation of this file.
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)
97 os << simox::alg::join(items,
", ") <<
" }";
101 template <
class Po
intT>
104 const pcl::PointCloud<PointT>& _pointCloud,
109 const pcl::PointCloud<PointT> pointCloud =
113 const bool local =
true;
115 for (
const PointT& point : pointCloud)
117 const Eigen::Vector3f vpoint(point.data);
126 voxel[
static_cast<Label>(point.label)]++;
134 LabelDensity::addPointCloud(*
this, pointCloud, pointCloudPose);
141 LabelDensity::addPointCloud(*
this, pointCloud, pointCloudPose);
148 for (
const Voxel& voxel : *
this)
150 for (
const auto& item : voxel)
152 ids.insert(item.first);
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)
190 const auto find = voxel.find(label);
191 return find != voxel.end() ? find->second : 0;
198 static io::VoxelAttributeSetterMap<Voxel>
199 makeJsonSetters(
const std::set<Label>& labels)
201 voxelgrid::io::VoxelAttributeSetterMap<Voxel> setters;
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&)
242 io::JsonIO::write<Voxel>(os, *
this, makeJsonGetters(*
this));
248 io::JsonIO::write<Voxel>(file, *
this, makeJsonGetters(*
this));
263 std::ifstream ifs(file);
282 static const std::string sep =
",";
288 std::vector<std::string> strings;
290 strings.push_back(
"index");
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;
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);
332 os << simox::alg::join(strings, sep) << std::endl;
340 std::ofstream os(file);
341 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.
MatrixXX< 4, 4, float > Matrix4f
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)
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...
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.
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