VoxelGrid.cpp
Go to the documentation of this file.
1 #include "VoxelGrid.h"
2 
3 #include <algorithm>
4 
6 
7 #include "../utils.h"
8 
10 {
11 
13  {
14  setFree();
15  }
16 
18  {
19  setOccupied(label);
20  }
21 
22  bool
23  Voxel::isFree() const
24  {
25  return !isOccupied();
26  }
27 
28  bool
30  {
31  return _isOccupied;
32  }
33 
34  Label
36  {
38  return _label;
39  }
40 
41  void
43  {
44  _isOccupied = false;
45  _label = 0;
46  }
47 
48  void
50  {
51  _isOccupied = true;
52  this->_label = label;
53  }
54 
55  void
56  to_json(nlohmann::json& json, const Voxel& voxel)
57  {
58  if (voxel.isOccupied())
59  {
60  json = voxel.getLabel();
61  }
62  else
63  {
64  json = -1;
65  }
66  }
67 
68  void
69  from_json(const nlohmann::json& json, Voxel& voxel)
70  {
71  if (json.get<int>() < 0)
72  {
73  voxel.setFree();
74  }
75  else
76  {
77  voxel.setOccupied(json.get<Label>());
78  }
79  }
80 
81  const std::string VoxelGrid::JSON_VOXEL_ARRAY_NAME = "labels";
82 
83  VoxelGrid
85  {
86  VoxelGrid grid(density.getStructure());
87  grid.setByLabelDensity(density);
88  return grid;
89  }
90 
91  std::set<Label>
93  {
94  std::set<Label> ids;
95  for (const Voxel& voxel : *this)
96  {
97  if (voxel.isOccupied())
98  {
99  ids.insert(voxel.getLabel());
100  }
101  }
102  return ids;
103  }
104 
105  void
106  VoxelGrid::setByLabelDensity(const LabelDensity::VoxelGrid& density, bool adoptStructure)
107  {
108  if (density.getStructure() != getStructure())
109  {
110  if (adoptStructure)
111  {
112  resetStructure(density.getStructure());
113  }
114  else
115  {
117  density.getStructure(),
118  getStructure(),
119  "Provided density grid has different structure, but adoptStructure is false.");
120  }
121  }
122 
123  for (std::size_t i = 0; i < numVoxels(); ++i)
124  {
125  const LabelDensity::Voxel& voxel = density.getVoxel(i);
126  if (voxel.isFree())
127  {
128  getVoxel(i).setFree();
129  }
130  else
131  {
132  getVoxel(i).setOccupied(voxel.getMaxLabel());
133  }
134  }
135  }
136 
137  void
138  VoxelGrid::writeJson(std::ostream& os) const
139  {
141  }
142 
143  void
144  VoxelGrid::writeJson(const std::filesystem::path& file) const
145  {
147  }
148 
149  void
150  VoxelGrid::readJson(std::istream& is)
151  {
153  }
154 
155  void
156  VoxelGrid::readJson(const std::filesystem::path& file)
157  {
159  }
160 
161  void
162  to_json(nlohmann::json& json, const VoxelGrid& grid)
163  {
165  }
166 
167  void
168  from_json(const nlohmann::json& json, VoxelGrid& grid)
169  {
171  }
172 
173 } // namespace visionx::voxelgrid::LabelOccupancy
visionx::voxelgrid::io::JsonIO::fromJson
static void fromJson(const nlohmann::json &j, VoxelGrid< VoxelT > &grid, const VoxelAttributeSetterMap< VoxelT > &attributeMap)
Deserialize a voxel grid from JSON.
Definition: JsonIO.h:295
visionx::voxelgrid::LabelOccupancy::to_json
void to_json(nlohmann::json &json, const Voxel &voxel)
Set json to the voxel's label if occupied, and to -1 if free.
Definition: VoxelGrid.cpp:56
visionx::voxelgrid::LabelOccupancy::Voxel::isFree
bool isFree() const
Indicate whether this voxel is free (i.e. it is not occupied).
Definition: VoxelGrid.cpp:23
visionx::voxelgrid::VoxelGrid
A 3D grid of voxels of type _VoxelT.
Definition: VoxelGrid.hpp:49
visionx::voxelgrid::io::JsonIO::toJson
static void toJson(nlohmann::json &j, const VoxelGrid< VoxelT > &grid, const VoxelAttributeGetterMap< VoxelT > &attributeMap)
Serialize a voxel grid to JSON.
Definition: JsonIO.h:246
visionx::voxelgrid::LabelDensity::Voxel::getMaxLabel
Label getMaxLabel() const
Get the label with most points.
Definition: VoxelGrid.cpp:46
visionx::voxelgrid::LabelOccupancy::Voxel::setFree
void setFree()
Set the voxel to free, i.e. unoccupied.
Definition: VoxelGrid.cpp:42
visionx::voxelgrid::LabelOccupancy::VoxelGrid::writeJson
void writeJson(std::ostream &os) const
Write this voxel grid to JSON.
Definition: VoxelGrid.cpp:138
visionx::voxelgrid::LabelOccupancy::VoxelGrid::getUniqueLabels
std::set< Label > getUniqueLabels() const
Get the set of unique labels in the grid.
Definition: VoxelGrid.cpp:92
visionx::voxelgrid::VoxelGrid< VoxelT >::resetStructure
void resetStructure(const VoxelGridStructure &structure)
Set the voxel grid structure and reset voxel data.
Definition: VoxelGrid.hpp:94
visionx::voxelgrid::LabelOccupancy
Definition: Visualizer.cpp:8
visionx::voxelgrid::LabelOccupancy::VoxelGrid
Voxel grid storing the number of points per label in each voxel.
Definition: VoxelGrid.h:62
visionx::voxelgrid::LabelDensity::VoxelGrid
Voxel grid storing the number of points per label in each voxel.
Definition: VoxelGrid.h:66
visionx::voxelgrid::io::JsonIO::writeJson
static void writeJson(const std::string &filename, const nlohmann::json &j, int indent=-1, char indentChar=' ')
Write JSON json to file.
Definition: JsonIO.cpp:8
visionx::voxelgrid::VoxelGrid::getStructure
VoxelGridStructure getStructure() const
Get the voxel grid structure.
Definition: VoxelGrid.hpp:87
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
visionx::voxelgrid::VoxelGrid::getVoxel
VoxelT & getVoxel(std::size_t index)
Get the voxel with the given index.
Definition: VoxelGrid.hpp:102
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
visionx::voxelgrid::VoxelGrid< VoxelT >::numVoxels
std::size_t numVoxels() const
Get the number of voxels in the grid.
Definition: VoxelGrid.hpp:175
visionx::voxelgrid::LabelOccupancy::Voxel::isOccupied
bool isOccupied() const
Indicate whether this voxel is occupied.
Definition: VoxelGrid.cpp:29
visionx::voxelgrid::LabelOccupancy::from_json
void from_json(const nlohmann::json &json, Voxel &voxel)
Set this voxel free, if json is < 0.
Definition: VoxelGrid.cpp:69
visionx::voxelgrid::LabelOccupancy::Voxel::setOccupied
void setOccupied(Label label)
Set the voxel to occupied and set its label.
Definition: VoxelGrid.cpp:49
visionx::voxelgrid::io::JsonIO::readJson
static nlohmann::json readJson(const std::string &filename)
Read JSON from file.
Definition: JsonIO.cpp:24
visionx::voxelgrid::LabelOccupancy::VoxelGrid::JSON_VOXEL_ARRAY_NAME
static const std::string JSON_VOXEL_ARRAY_NAME
Name of the single voxel array in JSON.
Definition: VoxelGrid.h:113
visionx::voxelgrid::LabelDensity::Voxel
Map of labels (object IDs) to number of points.
Definition: VoxelGrid.h:26
visionx::voxelgrid::LabelOccupancy::Voxel::Voxel
Voxel()
Construct an unoccupied voxel.
Definition: VoxelGrid.cpp:12
VoxelGrid.h
visionx::voxelgrid::LabelOccupancy::VoxelGrid::fromLabelDensity
static VoxelGrid fromLabelDensity(const LabelDensity::VoxelGrid &density)
Make a label occupancy grid from the given density grid.
Definition: VoxelGrid.cpp:84
visionx::voxelgrid::LabelOccupancy::VoxelGrid::readJson
void readJson(std::istream &is)
Read this voxel grid from JSON.
Definition: VoxelGrid.cpp:150
visionx::voxelgrid::LabelOccupancy::Voxel
A voxel storing whether it is occupied or free and the object label it is occupied by.
Definition: VoxelGrid.h:22
visionx::voxelgrid::error::InvalidStructure
Indicates that a voxel grid structure should have matched another one, but did not.
Definition: exceptions.h:39
JsonIO.h
visionx::voxelgrid::LabelOccupancy::Voxel::getLabel
Label getLabel() const
Get the label. Voxel must be occupied.
Definition: VoxelGrid.cpp:35
visionx::voxelgrid::LabelOccupancy::VoxelGrid::setByLabelDensity
void setByLabelDensity(const LabelDensity::VoxelGrid &density, bool adoptStructure=false)
Set the voxels by selecting the label with most points for each voxel.
Definition: VoxelGrid.cpp:106